1966번: 프린터 큐
여러분도 알다시피 여러분의 프린터 기기는 여러분이 인쇄하고자 하는 문서를 인쇄 명령을 받은 ‘순서대로’, 즉 먼저 요청된 것을 먼저 인쇄한다. 여러 개의 문서가 쌓인다면 Queue 자료구조에
www.acmicpc.net
일단 무작정 코드를 써내려 갔는데
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main()
{
int t, n, m, p, top, count = 0;
queue<int> q;
cin >> t;
for(int i=0; i<t; i++){
cin >> n >> m;
for(int j=0; j<n; j++){
cin >> p;
q.push(p);
if(j==0) top = p;
else if(p > top) top = p;
}
while(){
if(q[0] < top){
p = q[0];
q.pop();
q.push(p);
count++;
}
//q[0]이 가장 중요도 큰 문서일 때
else {
q.pop();
count++;
}
}
}
return 0;
}
내가 원하는 문서임을 표기할 방법이 없었다
그래서 아 queue<pair<int, int>>로 표현해줘야하는구나 하고 깨달음
그렇게 pair을 사용해서 변경해줌
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main()
{
int t, n, m, p, a, top;
queue<pair<int, int>> q;
cin >> t;
for(int i=0; i<t; i++){
int count = 0;
cin >> n >> m;
for(int j=0; j<n; j++){
cin >> p;
if(j != m) q.push({p, 0});
else q.push({p, 1});
cout << q.back().first << "," <<q.back().second << " ";
//중요도가 가장 큰 문서를 top에 저장
if(j==0) top = p;
else if(p > top) top = p;
}
while(!q.empty()){
if(q.front().second == 1 && ) {
count++;
break;
}
if(q.front().first < top){
p = q.front().first;
a = q.front().second;
q.pop();
q.push({p, a});
cout << "{"<< p << "," << a << "}";
count++;
}
//q[0]이 가장 중요도 큰 문서일 때
else {
q.pop();
count++;
}
}
while(!q.empty()) q.pop();
cout << count << "\n";
}
return 0;
}
근데... 개같이 실패
..
이렇게 우선순위 큐를 사용해서 간단하게 푸는 방법이 있더라
priority_queue<int> pq;
로 사용하면 됨
백준 1966 프린터 큐 [C++] :: 건호의 코딩공부 (tistory.com)
백준 1966 프린터 큐 [C++]
문제 링크 : https://www.acmicpc.net/problem/1966 소스코드 #include #include using namespace std;int main() { int count=0; int test_case; cin >> test_case; int n, m,ipt;//문서의 개수, 궁금한 문서 위치, 중요도 for (int i = 0; i < test_
numerok.tistory.com
'‡ CODING TEST STUDY ‡ > º 백준' 카테고리의 다른 글
[백준 1927번 C++] 최소힙 (0) | 2023.09.16 |
---|---|
[백준 11866번 C++] 요세푸스 문제 0 (0) | 2023.09.16 |
[백준 10866번 C++] 덱 (0) | 2023.09.13 |
[백준 2164번 C++] 카드 2 (0) | 2023.09.13 |
[백준 10845번 C++] 큐 (0) | 2023.09.12 |