BOJ1966
3879 단어 PriorityQueue큐PriorityQueue
BOJ 1966. 프린터 큐
문제
코드
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main(int argc, char const *argv[])
{
int tcase, n, m, nums, cnt;
cin >> tcase;
for (int i = 0; i < tcase; i++)
{
cnt = 0;
queue<pair<int, int> > q;
priority_queue<int> pq;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> nums;
q.push(make_pair(i, nums));
pq.push(nums);
}
while (!pq.empty())
{
int nowidx = q.front().first;
int nowval = q.front().second;
q.pop();
if (nowval == pq.top())
{
pq.pop();
cnt++;
if (nowidx == m)
{
cout << cnt << endl;
break;
}
}
else
{
q.push(make_pair(nowidx, nowval));
}
}
}
return 0;
}
코드2
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main(int argc, char const *argv[])
{
int tcase, n, m, nums, cnt;
cin >> tcase;
for (int i = 0; i < tcase; i++)
{
cnt = 0;
queue<pair<int, int>> q;
priority_queue<int> pq;
cin >> n >> m;
int tmp;
for (int i = 0; i < n; i++)
{
cin >> nums;
if (i == m)
tmp = nums;
pq.push(nums);
}
while (!pq.empty())
{
cnt++;
if (tmp == pq.top())
{
if (!pq.empty())
{
pq.pop();
continue;
}
}
pq.pop();
}
cout << cnt << endl;
}
return 0;
}
- 맨 처음 벡터로 접근하려다가 우선순위큐가 생각나서 priorityQueue로 문제를 풀었다.
하지만 119111과 같은 경우에서 특정 인덱스에 대한 순서를 정확히 구하지 못했다.
큐에도 pair
구조를 넣어서 사용할 수 있다는 것을 알게되었다. 🙂
Author And Source
이 문제에 관하여(BOJ1966), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@aksel26/BOJ1966저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)