[BOJ] 1966 프린터 큐

Time Lapse :17min 22sec

1966.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;

int N, M;

int print(){
int arr[10]={0, };
queue<pair<string,int>> q;
int doc;
for(int i=0;i<N;i++){
cin>>doc;
arr[doc] += 1;
if(i==M)
q.push(make_pair("this",doc));
else
q.push(make_pair("",doc));
}
int cnt = 1;
bool flag;
while(1){
int priority = q.front().second;
string s = q.front().first;
q.pop();
flag = true;
for(int i=priority+1;i<=9;i++){
if(arr[i]!=0){
flag = false;
break;
}
}
if(!flag){
q.push(make_pair(s,priority));
}
else{
if(s=="this")
break;
cnt++;
arr[priority] -= 1;
}
}
return cnt;
}
int main(void){
int test_case;
cin>>test_case;
for(int i=0;i<test_case;i++){
cin>>N>>M;
cout<<print()<<endl;
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/1966/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.