- 문제 링크 : boj.kr/15469
- 난이도 : G5
- 태그 : 스택, 큐, 시뮬레이션
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 0x7FFFFFFF
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
struct coin {
char mat;
ll cnt;
};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int T;
cin >> T;
while(T--) {
int n;
cin >> n;
queue<coin> _q;
stack<coin> _st;
for(int i = 0; i < n; i++) {
string s;
ll c;
cin >> s >> c;
_q.push({s[0], c});
}
while(!_q.empty()) {
coin curr = _q.front(); _q.pop();
if(!_st.empty()) {
coin bef = _st.top();
if(curr.mat == bef.mat) {
curr.cnt += bef.cnt;
_st.pop();
}
}
coin q, r;
q = {curr.mat, curr.cnt/3ll};
r = {curr.mat, curr.cnt%3ll};
if(q.cnt != 0) _q.push(q);
if(r.cnt != 0) _st.push(r);
}
ll ans = 0;
while(!_st.empty()) {
ans += _st.top().cnt;
_st.pop();
}
cout << ans << "\n";
}
return 0;
}
풀이
새로 넣어줄 동전은 큐에, 이미 넣은 동전은 스택에 저장하고 시뮬레이션 한다.
728x90
'PS' 카테고리의 다른 글
BOJ 1911 : 흙길 보수하기 (0) | 2024.07.27 |
---|---|
BOJ 2285 : 우체국 (0) | 2024.07.26 |
BOJ 19584 : 난개발 (1) | 2024.07.24 |
BOJ 8901 : 화학 제품 (1) | 2024.07.23 |
BOJ 20772 : Scheduler (2) | 2024.07.22 |