BOJ1966

김현민·2021년 1월 31일
0

Algorithm

목록 보기
18/126
post-thumbnail

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구조를 넣어서 사용할 수 있다는 것을 알게되었다. 🙂

profile
Jr. FE Dev

0개의 댓글