백준 118866번 - Silver IV
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int main(){
int n,k;
cin>>n>>k;
queue<int>q;//q 선언
for(int i=1;i<=n;i++){
q.push(i); // n명의 사람 입력
}
cout <<"<";
int cnt=1;
while(!q.empty()){
if(cnt%k==0){
int qfrnt = q.front();
q.pop();
if(q.empty())
cout <<qfrnt<<">";
else
cout <<qfrnt<<", ";
}
else{
int num = q.front();
q.pop();
q.push(num);
}
cnt++;
}
}
백준 1966번 - Silver III
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int main(){
int n=0,m=0,imp,tc=0;
int cnt=0;
cin>>tc;//Testcase
for(int i=0;i<tc;i++){ //tc만큼 문서개수와 , 찾는 문서 위치 입력하기
cnt=0;
queue<pair<int,int>>q;//q 선언
priority_queue<int>pq; //우선순위 큐
cin>>n>>m; // 문서 수 , 찾는 문서 위치
for(int j=0;j<n;j++){ //문서 수 만큼 중요도 입력
cin>>imp;//중요도
q.push({j,imp}); // 큐에 문서 순서, 중요도 입력
pq.push(imp); // 우선순위 큐에 중요도 입력
}
while(!q.empty()){
int k=q.front().first;//j
int f=q.front().second;//imp
q.pop();
if(pq.top()==f){ //imp가 f와 동일하다면
pq.pop();
cnt++;
if(k==m){ //찾는 문서 위치와 같으면
cout <<cnt<<endl;
break;
}
}else{
q.push({k,f}); // 다시 큐 뒤로 넣기
}
}
}
}
덱(dequeue) 문제는 일단 넘어가겠습니다
'코딩 > C++' 카테고리의 다른 글
| 백준 정렬 모음집 - 2 (0) | 2022.05.16 |
|---|---|
| 백준 정렬 모음집 - 1 (0) | 2022.05.16 |
| C++ 알고리즘 - 백준 Stack , Queue (0) | 2022.04.06 |
| C++ 알고리즘 - 백준 stack (0) | 2022.03.31 |
| C++ 알고리즘 - 이진검색의 시간복잡도 (0) | 2022.03.29 |