출처: https://bumcrush.tistory.com/182 [맑음때때로 여름]
https://www.acmicpc.net/problem/1966
1966번: 프린터 큐
문제 여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료��
www.acmicpc.net
#include <iostream>
#include <queue>
using namespace std;
int N, M;
int cnt;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int num;
cin >> num;
while (num--)
{
queue<pair<int, int>> q; //중요도, index
priority_queue<int> pq;
cin >> N >> M;
for (int i = 0; i < N; i++)
{
int tmp;
cin >> tmp;
q.push(make_pair(tmp, i));
pq.push(tmp);
}
while (!q.empty())
{
int priority = q.front().first;
int index = q.front().second;
q.pop();
if (pq.top() == priority)
{
pq.pop();
cnt++;
if (index == M)
{
cout << cnt << "\n";
cnt = 0;
break;
}
}
else
{
q.push(make_pair(priority, index));
}
}
}
}
queue와 priority queue를 같이 사용하면 쉽게 풀 수 있는 문제였다.
queue, priority queue, 그리고 이 문제에 대한 정보는
https://en.cppreference.com/w/cpp/container/queue
https://en.cppreference.com/w/cpp/container/priority_queue
http://melonicedlatte.com/algorithm/2018/02/18/230705.html
이곳들에서 참조했다.
짚고 넘어가야할 가장 중요한 점은 queue에서 front가 priority queue에서는 top이 된다는 것이 었다. 또한 stack이나 queue에는 clear 기능이 존재하지 않는데 이 때문에 stack 혹은 queue를 비울 때는 while(!q.empty()) q.pop() 과 같은 연산으로 초기화 시켜주어야 한다.
BFS, DFS (0) | 2020.07.19 |
---|---|
백준 1748 수 이어 쓰기 1(brute force) (0) | 2020.07.18 |
백준 7568 덩치(brute force) (0) | 2020.07.16 |
백준 4641 Doubles(brute force) (0) | 2020.07.16 |
백준 2858 기숙사 바닥(brute force) (0) | 2020.07.16 |
댓글 영역