๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 23294_์›น ๋ธŒ๋ผ์šฐ์ € 1

๋ฟŒ์•ผ._. 2023. 8. 21. 11:33

Gold IV

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/23294)

< ์›น ๋ธŒ๋ผ์šฐ์ € 1 >

 

๋ฌธ์ œ ํ’€์ด 

 

Deque 2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋’ค๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„๊ณผ ์•ž์œผ๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์„ ๋‚˜๋ˆ„์–ด ๊ตฌํ˜„ํ–ˆ๋‹ค.

๊ฐ ์ž‘์—…์„ ์‹คํ–‰ํ•  ๋•Œ๋งˆ๋‹ค ์บ์‹œ ์šฉ๋Ÿ‰๋„ ๋’ค๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„๊ณผ ์•ž์œผ๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์„ ๋‚˜๋ˆ„์–ด ๊ณ„์‚ฐํ•ด ์ฃผ์—ˆ๋‹ค.

 

 

 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.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
import java.util.StringTokenizer;

public class _23294_ { // ์›น ๋ธŒ๋ผ์šฐ์ € 1

	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 = new StringTokenizer(bf.readLine());

		int n = Integer.parseInt(st.nextToken());
		int q = Integer.parseInt(st.nextToken());
		int c = Integer.parseInt(st.nextToken());

		int cap[] = new int[n + 1];
		st = new StringTokenizer(bf.readLine());
		for (int i = 1; i < n + 1; i++) {
			cap[i] = Integer.parseInt(st.nextToken());
		}

		Deque<Integer> back = new ArrayDeque<>();
		Deque<Integer> front = new ArrayDeque<>();

		int now = -1;
		int front_size = 0;
		int back_size = 0;

		for (int i = 0; i < q; i++) {
			st = new StringTokenizer(bf.readLine());
			char cmd = st.nextToken().charAt(0);

			if (cmd == 'B') {
				if (!back.isEmpty()) {
					front.add(now);
					front_size += cap[now];
					back_size -= cap[back.peekLast()];
					now = back.pollLast();
				}
			} else if (cmd == 'F') {
				if (!front.isEmpty()) {
					back.add(now);
					back_size += cap[now];
					front_size -= cap[front.peekLast()];
					now = front.pollLast();
				}
			} else if (cmd == 'A') {
				int num = Integer.parseInt(st.nextToken());
				front_size = 0;
				front.clear();

				if (now != -1) {
					back.add(now);
					back_size += cap[now];
				}

				now = num;

				while (front_size + back_size + cap[now] > c) {
					back_size -= cap[back.pollFirst()];
				}
			} else if (cmd == 'C') {
				Deque<Integer> temp = new ArrayDeque<>();
				for (int x : back) {
					if (temp.isEmpty()) {
						temp.add(x);
					} else {
						if (temp.peekLast() != x) {
							temp.add(x);
						} else {
							back_size -= cap[x];
						}
					}
				}
				back = temp;
			}
		}

		bw.write(now + "\n");

		if (back.isEmpty()) {
			bw.write(-1+"\n");
		} else {
			Iterator<Integer> iter1 = back.descendingIterator();
			while (iter1.hasNext()) {
				bw.write(iter1.next() + " ");
			}
			bw.write("\n");
		}


		if (front.isEmpty()) {
			bw.write(-1+"\n");
		} else {
			Iterator<Integer> iter2 = front.descendingIterator();
			while (iter2.hasNext()) {
				bw.write(iter2.next() + " ");
			}
		}
		bw.flush();
	}
}

 

Main

๋ณ€์ˆ˜)
n : ์›นํŽ˜์ด์ง€์˜ ์ข…๋ฅ˜์˜ ์ˆ˜
q : ์ž‘์—…์˜ ๊ฐœ์ˆ˜
c : ์ตœ๋Œ€ ์บ์‹œ ์šฉ๋Ÿ‰
cap : ๊ฐ ์›นํŽ˜์ด์ง€์˜ ์บ์‹œ ๊ณต๊ฐ„
back : Deque <Integer> ๋’ค๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„
front : Deque <Integer> ์•ž์œผ๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„
now : ํ˜„์žฌ ํŽ˜์ด์ง€
front_size, back_size : ์•ž์œผ๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์˜ ์บ์‹œ ์šฉ๋Ÿ‰, ๋’ค๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์˜ ์บ์‹œ ์šฉ๋Ÿ‰
cmd : ๊ฐ ์ž‘์—…

 

- ์›น ํŽ˜์ด์ง€์˜ ์ข…๋ฅ˜์˜ ์ˆ˜(n), ์‚ฌ์šฉ์ž๊ฐ€ ์ˆ˜ํ–‰ํ•˜๋Š” ์ž‘์—…์˜ ๊ฐœ์ˆ˜(q), ์ตœ๋Œ€ ์บ์‹œ ์šฉ๋Ÿ‰(c) ์ž…๋ ฅ

- ์›น ํŽ˜์ด์ง€์˜ ์ข…๋ฅ˜์˜ ์ˆ˜(n)๋งŒํผ ๊ฐ ์›น ํŽ˜์ด์ง€์˜ ์บ์‹œ ๊ณต๊ฐ„์„ ์ž…๋ ฅ๋ฐ›์•„ cap ๋ฐฐ์—ด์— ์ €์žฅ

- ์ž‘์—…์˜ ๊ฐœ์ˆ˜(q)๋งŒํผ ์ž‘์—… ์ž…๋ ฅ๋ฐ›์•„ ์ˆ˜ํ–‰

: B๋ผ๋ฉด ๋’ค๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์ด ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธ ํ›„ ๋น„์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด ํ˜„์žฌ ํŽ˜์ด์ง€๋ฅผ ์•ž์œผ๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์— ์ €์žฅ ํ›„ ์•ž์œผ๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์˜ ์บ์‹œ ์šฉ๋Ÿ‰ ์ถ”๊ฐ€ ๋ฐ ๋’ค๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์—์„œ ๊ฐ€์žฅ ์ตœ๊ทผ์˜ ํŽ˜์ด์ง€(pollLast)์— ์ ‘์† 

: F๋ผ๋ฉด ์•ž์œผ๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์ด ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธ ํ›„ ๋น„์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด ํ˜„์žฌ ํŽ˜์ด์ง€๋ฅผ ๋’ค๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์— ์ €์žฅ ํ›„ ๋’ค๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์˜ ์บ์‹œ ์šฉ๋Ÿ‰ ์ถ”๊ฐ€ ๋ฐ ์•ž์œผ๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์—์„œ ๊ฐ€์žฅ ์ตœ๊ทผ์˜ ํŽ˜์ด์ง€(pollLast)์— ์ ‘์†

: A๋ผ๋ฉด

1) ์•ž์œผ๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์— ์ €์žฅ๋œ ํŽ˜์ด์ง€๊ฐ€ ๋ชจ๋‘ ์‚ญ์ œ๋˜๋ฏ€๋กœ clear ๋ฐ ์•ž์œผ๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์˜ ์บ์‹œ ์šฉ๋Ÿ‰ 0์œผ๋กœ ์ €์žฅ

2) ์ฒ˜์Œ์œผ๋กœ ์›น ํŽ˜์ด์ง€์— ์ ‘์†ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด(now!=-1) ํ˜„์žฌ ํŽ˜์ด์ง€๋ฅผ ๋’ค๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์— ์ถ”๊ฐ€

3) ์ฒ˜์Œ์œผ๋กœ ์ ‘์†์ธ์ง€ ์ƒ๊ด€์—†์ด ์›น ํŽ˜์ด์ง€ ์ ‘์†

4) ์‚ฌ์šฉ ์ค‘์ธ ์บ์‹œ ์šฉ๋Ÿ‰ (์•ž์œผ๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„ + ๋’ค๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„ + ํ˜„์žฌ ์ ‘์† ์ค‘์ธ ํŽ˜์ด์ง€)์ด ์ตœ๋Œ€ ์บ์‹œ ์šฉ๋Ÿ‰(c)์„ ์ดˆ๊ณผํ•˜๋Š” ๊ฒฝ์šฐ ๋’ค๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์—์„œ ๊ฐ€์žฅ ์˜ค๋ž˜๋œ ํŽ˜์ด์ง€ ์‚ญ์ œ(pollFirst)

: C๋ผ๋ฉด ๋’ค๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์„ ์ „์ฒด ํƒ์ƒ‰ํ•˜๋ฉฐ ๊ฐ™์€ ๋ฒˆํ˜ธ์˜ ํŽ˜์ด์ง€๊ฐ€ ์—ฐ์†ํ•ด์„œ 2๊ฐœ ์ด์ƒ ๋“ฑ์žฅํ•  ๊ฒฝ์šฐ ํ•˜๋‚˜๋งŒ ๋‚จ๊ธฐ๊ณ  ์ œ๊ฑฐ

- ํ˜„์žฌ ์ ‘์† ์ค‘์ธ ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ ์ถœ๋ ฅ

- ๋’ค๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์ด ๋น„์–ด์žˆ๋‹ค๋ฉด -1 ์ถœ๋ ฅ, ๋น„์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด ๊ฐ€์žฅ ์ตœ๊ทผ์— ๋ฐฉ๋ฌธํ•œ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ๋’ค์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•ด์„œ ์ถœ๋ ฅ

- ์•ž์œผ๋กœ ๊ฐ€๊ธฐ ๊ณต๊ฐ„์ด ๋น„์–ด์žˆ๋‹ค๋ฉด -1 ์ถœ๋ ฅ, ๋น„์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด ๊ฐ€์žฅ ์ตœ๊ทผ์— ๋ฐฉ๋ฌธํ•œ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด ๋’ค์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•ด์„œ ์ถœ๋ ฅ