๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/5957)
< Cleaning the Dishes >
๋ฌธ์ ํ์ด
Stack 3๊ฐ๋ฅผ ํ์ฉํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ค.
1) ์ ์ป์ ์ ์
2) ์ป์ ์ ์
3) ๋ฆ์ ์ ์
my solution (Java)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Stack;
import java.util.StringTokenizer;
public class _5957_ { // Cleaning the Dishes
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
int n = Integer.parseInt(bf.readLine());
Stack<Integer> unwashed = new Stack<>();
Stack<Integer> washed = new Stack<>();
Stack<Integer> dried = new Stack<>();
for (int i = n; i > 0; i--) {
unwashed.add(i);
}
String str = "";
while ((str = bf.readLine()) != null) {
st = new StringTokenizer(str);
int c = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
if (c == 1) {
for (int i = 0; i < d; i++) {
washed.add(unwashed.pop());
}
} else {
for (int i = 0; i < d; i++) {
dried.add(washed.pop());
}
}
}
while (!dried.isEmpty()) {
bw.write(dried.pop() + "\n");
}
bw.flush();
}
}
๋ณ์)
n : ์ ์ ๊ฐ์
unwashed, washed, dried : ์ ์ป์ ์ ์, ์ป์ ์ ์, ๋ฆ์ ์ ์
c, d : ๋ช ๋ น, ์ ์ ๊ฐ์
์ ์ ๊ฐ์๋ฅผ ์ ๋ ฅ๋ฐ์ ์ ์ป์ ์ ์ stack์ ๊ฐ์๋งํผ ์์๋๋ก ์ ์ฅํ๋ค. [๋ช ๋ น, ์ ์ ๊ฐ์]๋ฅผ ์ ๋ ฅ๋ฐ์ ๋ค์ ๊ณผ์ ์ ๊ฑฐ์น๋ค.
1) c ๊ฐ์ด 1์ด๋ผ๋ฉด d๊ฐ๋งํผ ์ ์๋ฅผ ์ป๋๋ค. (unwashed -> washed)
2) c ๊ฐ์ด 2๋ผ๋ฉด d๊ฐ๋งํผ ์ ์๋ฅผ ๋ฆ๋๋ค. (washed -> dried)
์ต์ข dried stack์ ๋ค ๊บผ๋ด ์ถ๋ ฅํ๋ค.

'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Baekjoon] 14540_Railway Station (0) | 2025.11.12 |
|---|---|
| [Baekjoon] 4992_Hanafuda Shuffle (0) | 2025.11.10 |
| [Baekjoon] 3277_DOMAINS (0) | 2025.11.07 |
| [Baekjoon] 10106_The Geneva Confection (0) | 2025.11.06 |
| [Baekjoon] 6379_Scramble Sort (0) | 2025.11.05 |