๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 26042_์‹๋‹น ์ž…๊ตฌ ๋Œ€๊ธฐ ์ค„

๋ฟŒ์•ผ._. 2023. 8. 10. 14:26

Silver V

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

< ์‹๋‹น ์ž…๊ตฌ ๋Œ€๊ธฐ ์ค„ >

 

๋ฌธ์ œ ํ’€์ด 

 

Queue๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•™์ƒ์ด ๋Œ€๊ธฐํ•  ๋•Œ๋งˆ๋‹ค ๋Œ€๊ธฐํ•˜๋Š” ํ•™์ƒ ์ˆ˜์™€ ๋งจ ๋’ค์— ์ค„ ์„œ ์žˆ๋Š” ํ•™์ƒ ๋ฒˆํ˜ธ๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋‹ต์„ ์ฐพ๋Š”๋‹ค.

 

 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.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class _26042_ { // ์‹๋‹น ์ž…๊ตฌ ๋Œ€๊ธฐ ์ค„

	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());

		Queue<Integer> queue = new LinkedList<>();
		int cnt = 0;
		int num = 100000;

		for (int i = 0; i < n; i++) {
			st = new StringTokenizer(bf.readLine());
			int type = Integer.parseInt(st.nextToken());

			if (type == 1) {
				int temp = Integer.parseInt(st.nextToken());
				queue.add(temp);
				if (queue.size() >= cnt) {
					if (queue.size() == cnt) {
						if (num > temp)
							num = temp;
					} else {
						cnt = queue.size();
						num = temp;
					}
				}
			} else {
				queue.poll();
			}
		}
		bw.write(cnt +" "+num);
		bw.flush();
	}
}

 

Main

๋ณ€์ˆ˜)
n : ์ •๋ณด ์ˆ˜
queue : ๋Œ€๊ธฐ ํ•™์ƒ ๋ฒˆํ˜ธ ์ €์žฅ
cnt : ๋Œ€๊ธฐํ•˜๋Š” ํ•™์ƒ ์ˆ˜ ์ตœ๋Œ€
num : ๋Œ€๊ธฐํ•˜๋Š” ํ•™์ƒ ์ˆ˜ ์ตœ๋Œ€์ผ ๋•Œ ๋งจ ๋’ค์— ์ค„ ์„œ ์žˆ๋Š” ํ•™์ƒ ๋ฒˆํ˜ธ
type : 1 or 2
temp : ๋Œ€๊ธฐํ•˜๋Š” ํ•™์ƒ ๋ฒˆํ˜ธ ์ž…๋ ฅ

 

- ์ •๋ณด ์ˆ˜ (n) ์ž…๋ ฅ

- ์ •๋ณด ์ˆ˜๋งŒํผ ์ •๋ณด ์ž…๋ ฅ

: ์œ ํ˜•์ด 1์ด๋ผ๋ฉด ๋Œ€๊ธฐ์ค„์— ์ถ”๊ฐ€๋˜๋ฏ€๋กœ size๋ฅผ ํ™•์ธํ•ด ๋Œ€๊ธฐํ•˜๋Š” ํ•™์ƒ ์ˆ˜ ์ตœ๋Œ€์ธ์ง€ ํ™•์ธ -> ์ตœ๋Œ€ ๊ฐ’๊ณผ ๊ฐ™๋‹ค๋ฉด ํ•™์ƒ ๋ฒˆํ˜ธ๊ฐ€ ์ž‘์€ ๊ฒƒ์œผ๋กœ ์—…๋ฐ์ดํŠธ/ ์ตœ๋Œ€ ๊ฐ’๋ณด๋‹ค ๋” ํฌ๋‹ค๋ฉด ํ•™์ƒ ์ˆ˜์™€ ๋ฒˆํ˜ธ ์—…๋ฐ์ดํŠธ

: ์œ ํ˜•์ด 2๋ผ๋ฉด queue.poll()

- ์ตœ๋Œ€๊ฐ€ ๋˜์—ˆ๋˜ ์ˆœ๊ฐ„์˜ ํ•™์ƒ ์ˆ˜(cnt)์™€ ๊ทธ๋•Œ ๋งจ ๋’ค ํ•™์ƒ ๋ฒˆํ˜ธ(num) ์ถœ๋ ฅ

 



'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 11000_๊ฐ•์˜์‹ค ๋ฐฐ์ •  (0) 2023.08.14
[Baekjoon] 24511_queuestack  (0) 2023.08.11
[Baekjoon] 28107_ํšŒ์ „์ดˆ๋ฐฅ  (0) 2023.08.09
[Baekjoon] 1726_๋กœ๋ด‡  (0) 2023.08.08
[Baekjoon] 5464_์ฃผ์ฐจ์žฅ  (0) 2023.08.08