주어진 조건을 얼마나 정확하게 이해하는 것이 중요한 문제였다.

먼저, 문제를 읽고 아래와 같이 명령어에 따라 선택해야 하는 수행 동작을 정리하였다. 

1.deposit : k금액 입금 후 결제 대기 목록에 따라 결제 진행
2.pay : 계좌에 k이상 금액이 있는 경우 k만큼 결제
3.reservation
3-1)계좌에 k이상 금액이 있는 경우 k만큼 결제
3-2)계좌에 금액이 부족한 경우 결제 대기 목록에 추가(선입선출을 위해 큐를 사용)



구현 코드는 아래와 같다.

#include <iostream>
#include <queue>
using namespace std;

int n, m, cost;
string command;

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	queue<int> payWating;
	for (int i = 0; i < m; i++) {
		cin >> command >> cost;
		if (command == "deposit") {
			n += cost;
			if (!payWating.empty() && n >= payWating.front()) { 
				n -= payWating.front(); 
				payWating.pop();
			}
		}
		else if (command == "pay" && cost <= n) n -= cost;
		else if (command == "reservation") {
			if (n >= cost) n -= cost;
			else payWating.push(cost);
		}
	}
	cout << n;
	return 0;
}

해당 코드로 제출 한 결과 3개의 케이스에서 FAIL을 받았다.

문제를 다시 한번 읽어 보며 놓친 조건이 무엇인지 생각해 보았고 reservation시 계좌에 돈이 충분해도 대기 중인 결제가 하나라도 존재하면 결제하지 않고 결제 대기 목록에 추가해야 한다는 조건을 만족하도록 로직을 수정해 주었다.

#include <iostream>
#include <queue>
using namespace std;

int n, m, cost;
string command;

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	queue<int> payWating;
	for (int i = 0; i < m; i++) {
		cin >> command >> cost;
		if (command == "deposit") {
			n += cost;
			if (!payWating.empty() && n >= payWating.front()) { 
				n -= payWating.front(); 
				payWating.pop();
			}
		}
		else if (command == "pay" && cost <= n) n -= cost;
		else if (command == "reservation") {
			if (n >= cost && payWating.empty()) n -= cost;
			else payWating.push(cost);
		}
	}
	cout << n;
	return 0;
}

여전히 13, 14, 15 케이스에서 정답을 출력하지 못하고 있었고 deposit 입금 시 결제 대기 목록에서 더 이상 결제가 불가능 할 때까지 결제를 수행해야 된다는 것을 깨달았다. 

 

최종적으로 아래와 같이 수정하여 문제를 해결하였다.

#include <iostream>
#include <queue>
using namespace std;

int n, m, cost;
string command;

int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n >> m;
	queue<int> payWating;
	for (int i = 0; i < m; i++) {
		cin >> command >> cost;
		if (command == "deposit") {
			n += cost;
			while (!payWating.empty()) {
				if (n >= payWating.front()) {
					n -= payWating.front();
					payWating.pop();
				}
				else break;
			}
		}
		else if (command == "pay" && cost <= n) n -= cost;
		else if (command == "reservation") {
			if (n >= cost && payWating.empty()) n -= cost;
			else payWating.push(cost);
		}
	}
	cout << n;
	return 0;
}

문제를 읽을 때 조건을 좀 더 주의 깊게 파악해야겠다.