일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- validation
- jwt
- spring boot
- 패러다임 불일치
- wss 연결 실패
- ec2 nginx websocket reverse proxy
- 이미지 업로드
- presigned url
- 관점 지향 프로그래밍
- 스프링부트
- 백준 10815 # 백준 Java
- 자바 orm
- AWS
- 도메인 주도 개발
- Flask
- fastapi
- GoormIDE
- oauth2.0
- 구글 로그인
- 예외 처리
- 개발 프로젝트
- @Valid
- session
- springboot
- logout
- OpenAI API
- spring websocket nginx 설정
- 소셜 로그인
- CustomException
- S3
Archives
- Today
- Total
개발세발은 안되요
[C++] 백준 1966: 프린터 큐 본문
문제
https://www.acmicpc.net/problem/1966
풀이
- 문서의 중요도와 문서의 순서를 queue에 넣는다. 동시에 priority_queue에 문서의 중요도만 넣어준다.
- priority_queue의 top() 값과 queue의 front()값을 비교하여 현재 인쇄하려는 문서보다 중요도가 높은 문서가 있는지를 확인한다.
- 만약 문서를 인쇄할 수 있는 경우, priority_queue를 pop() 한다.
- 만약 문서를 인쇄할 수 없는 경우, queue의 front() 값을 다시 queue에 push 해준다.
코드
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Num{
int value, index;
};
int main()
{
int t, n , m;
vector<int> v;
cin >> t;
for(int i=0; i<t; i++){
cin >> n >> m;
queue<Num> q;
priority_queue<int> q_num;
for(int i=0; i<n; i++){
int num;
cin >> num;
q.push({num, i}); // 현재 가치와 index를 push
q_num.push(num); // 가치를 push. 내림차순 정렬.
}
int ans=1;
while(!q.empty()){
// q의 맨 앞 원소를 뺀다.
int cur_value = q.front().value;
int cur_index = q.front().index;
q.pop();
// 만약 현재 원소보다 가치 큰 값이 있는 경우, 맨 뒤로 push
if(cur_value <q_num.top()){
q.push({cur_value, cur_index});
}
// 현재 값을 뽑을 수 있는 경우
else{
// 현재 원소가 우리가 구해야 값이라면
if(cur_index == m){
v.push_back(ans);
break;
}
q_num.pop();
ans++; // 카드 뽑은 횟수 증가
}
}
}
for(int i=0; i<v.size(); i++){
cout << v[i] << "\n";
}
return 0;
}
'알고리즘 > 백준' 카테고리의 다른 글
[C++] 백준 17829 : 222-풀링 (0) | 2025.04.10 |
---|---|
[C++] 백준 2075 : N번째 큰 수 (0) | 2025.04.02 |
[C++] 백준 19598 : 최소 회의실 개수 (0) | 2025.03.31 |
[C++] 백준 13549 : 숨바꼭질3 (0) | 2025.03.27 |
[C++] 백준 20164 : 홀수 홀릭 호석 (0) | 2025.03.26 |