๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 5957_Cleaning the Dishes

๋ฟŒ์•ผ._. 2025. 11. 14. 10:59
๋ฌธ์ œ(์ถœ์ฒ˜: 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