while(1) 작심삼일();

[백준 1966번] 프린터 큐 본문

CS/baekjoon

[백준 1966번] 프린터 큐

hanjongho 2021. 4. 19. 13:41

https://www.acmicpc.net/problem/1966

 

1966번: 프린터 큐

여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료구조에

www.acmicpc.net

풀이) 

queue 라이브러리를 사용하려했으나 idx접근하는 메소드가 없어서 vector로 구현했다. 문제에서 요구한 내용을 하나씩 구현해서 해결할 수 있었다. 알아내려는 인덱스가 맞고, 뒤에 나보다 큰 것도 없으면 멈추고 그 당시의 cnt값을 출력해주면 된다.

#include <iostream>
#include <vector>
using namespace std;

int T;

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	cin >> T;
	while (T--)
	{
		int N, M, tmp;
		vector<pair<int, int> > printer;

		cin >> N >> M;
		for (int i = 0; i < N; i++)
		{
			cin >> tmp;
			printer.push_back(make_pair(tmp, i));
		}

		int cnt = 1;
		while (!printer.empty())
		{
			int push_check = false;
			int now = printer.front().first;
			int s_idx = printer.front().second;
			printer.erase(printer.begin());
			for (int i = 0; i < printer.size(); i++)
			{
				if (printer[i].first > now)
				{
					printer.push_back(make_pair(now, s_idx));
					push_check = true;
					break ;
				}
			}
			if (!push_check && s_idx == M)
				break ;
			else if (!push_check)
				cnt++;
		}
		cout << cnt << "\n";
	}
	
	return (0);
}

 

 

제 코드는 절대 완벽하지 않습니다

더 좋은 풀이방법 혹은 코드에 관한 질문이 있으시면 언제든 댓글주세요!

'CS > baekjoon' 카테고리의 다른 글

[백준 1541번] 잃어버린 괄호  (0) 2021.04.19
[백준 2559번] 수열  (0) 2021.04.19
[백준 1874번] 스택 수열  (0) 2021.04.19
[백준 2470번] 두 용액  (0) 2021.04.19
[백준 7569번] 토마토  (0) 2021.04.17
Comments