Gidhub BE Developer

[BOJ] 1966. 프린터 큐

2018-09-27
goodGid

Problem

Problem URL : 프린터 큐


[1] Answer Code (18. 09. 27)

#include <string>
#include <vector>
#include <queue>
#include <iostream>
#define p pair<int,int>
using namespace std;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    
    int tc;
    cin >> tc;
    while (tc--) {
        int n;
        int location;
        cin >> n >> location;
     
        vector<int> priorities;
        for(int i=0; i<n ;i++){
            int tmp;
            cin >> tmp;
            priorities.push_back(tmp);
        }
        
        int answer = 0;
        queue<p> q;
        priority_queue<int> pq;
        int size = (int) priorities.size();
        for(int i=0; i<size; i++){
            q.push( {priorities[i],i} );
            pq.push(priorities[i]);
        }
        
        while(! q.empty()){
            int value = q.front().first;
            int idx = q.front().second;
            q.pop();
            if( pq.top() == value){
                pq.pop();
                answer++;
                if(idx == location){
                    cout << answer << endl;
                    break;
                }
            }
            else{
                q.push( {value,idx} );
            }
        }
    }
    return 0;
}

Review


Recommend

Index