[문제 링크] : https://www.acmicpc.net/problem/20055
#include <iostream>
#include <queue>
using namespace std;
int Arr[201];
bool Check[201];
queue<int> Q;
int Ans, Start, End, T;
int main(){
int N, K;
cin >> N >> K;
Start = 1;
End = N;
for(int i=1; i<=N*2; i++){
cin >> Arr[i];
}
while(Ans < K){
T++;
Start = Start == 1 ? N * 2 : Start - 1;
End = End == 1 ? N * 2 : End - 1;
int sz = Q.size();
for(int i=0; i<sz; i++){
int cur = Q.front();
Q.pop();
if(cur == End){
Check[cur] = false;
continue;
}
int next = cur == 2 * N ? 1 : cur + 1;
if(Arr[next] == 0 || Check[next] == true){
Q.push(cur);
}
else if(next == End){
Check[cur] = false;
Arr[next]--;
if(Arr[next] == 0){
Ans++;
}
}
else{
Q.push(next);
Check[cur] = false;
Check[next] = true;
Arr[next]--;
if(Arr[next] == 0){
Ans++;
}
}
}
if(Arr[Start] > 0){
Q.push(Start);
Check[Start] = true;
Arr[Start]--;
if(Arr[Start] == 0){
Ans++;
}
}
}
cout << T << "\n";
return 0;
}
'알고리즘' 카테고리의 다른 글
[알고리즘] 백준 23235 - The Fastest Sorting Algorithm In The World (C++) (0) | 2025.01.04 |
---|---|
[알고리즘] 백준 5427 - 불 (C++) (0) | 2025.01.03 |
[알고리즘] 백준 6378 - 디지털 루트 (C++) (0) | 2025.01.01 |
[알고리즘] 백준 17356 - 욱 제 (C++) (0) | 2024.12.31 |
[알고리즘] 백준 1030 - 프렉탈 평면 (C++) (0) | 2024.12.30 |