[boj] 1966 프린터 큐
Problem Solving
2020. 12. 14. 01:03
반응형
1966번: 프린터 큐
여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료구조에
www.acmicpc.net
#include <iostream>
#include <deque>
#include <stack>
#include <queue>
using namespace std;
queue<pair<int, int>> mem; // 인덱스 메모리
priority_queue<int> prior; // 우선순위
int test;
int n;
int target;
int paper;
int cnt;
/*
** 인덱스 메모리에 인덱스와 우선순위를 넣는다.
** priority_queue를 활용하여 먼저 인쇄할 것을 결정한다.
*/
int main()
{
cin >> test;
while (test--)
{
cin >> n >> target;
for (int i = 0; i < n; i++)
{
cin >> paper;
mem.push({i, paper});
prior.push(paper);
}
cnt = 0;
while (42)
{
if (prior.top() == mem.front().second)
{
cnt++;
if (target == mem.front().first)
break ;
prior.pop();
mem.pop();
}
else
{
mem.push({mem.front().first, mem.front().second});
mem.pop();
}
}
while (!mem.empty())
mem.pop();
while (!prior.empty())
prior.pop();
cout << cnt << endl;
}
return (0);
}
동료에게서 도움을 받아 보완한 함수이다.
이게 아무래도 우선순위와 위치를 같이 생각해야하다보니 쌍으로 데이터를 저장하는게
머리가 안아프다... 이런건 기계를 시켜야한다.
그래서 queue의 자료형을 pair로 받고 앞에는 위치를, 뒤에는 우선순위를 넣어서 해결을 보았다.
반응형
'Problem Solving' 카테고리의 다른 글
[boj] 5430 AC (0) | 2020.12.23 |
---|---|
[boj] 1406 에디터 (0) | 2020.12.15 |
[boj] 5397 키로거 (0) | 2020.12.12 |
[boj] 2346 풍선 터뜨리기 (0) | 2020.12.11 |
[boj] 1935 후위 표기식2 (0) | 2020.12.08 |