๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1713_ํ›„๋ณด ์ถ”์ฒœํ•˜๊ธฐ

๋ฟŒ์•ผ._. 2023. 11. 8. 15:27

Silver I

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

< ํ›„๋ณด ์ถ”์ฒœํ•˜๊ธฐ >

 

๋ฌธ์ œ ํ’€์ด 

 

ArrayList๋ฅผ 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.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;

public class _1713_ { // ํ›„๋ณด ์ถ”์ฒœํ•˜๊ธฐ
	static class Draw {
		private int person;
		private int cnt;
		private int seq;

		private Draw(int person, int cnt, int seq) {
			this.person = person;
			this.cnt = cnt;
			this.seq = seq;
		}
	}

	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());
		int m = Integer.parseInt(bf.readLine());

		ArrayList<Draw> list = new ArrayList<>();
		ArrayList<Integer> arr = new ArrayList<>();
		int idx = 0;

		st = new StringTokenizer(bf.readLine());
		for (int i = 0; i < m; i++) {
			int num = Integer.parseInt(st.nextToken());
			if (arr.contains(num)) {
				for (int j = 0; j < list.size(); j++) {
					if (list.get(j).person == num) {
						list.get(j).cnt += 1;
					}
				}
			} else {
				if (list.size() >= n) {
					Collections.sort(list, new Comparator<Draw>() {
						@Override
						public int compare(Draw o1, Draw o2) {
							if (o1.cnt == o2.cnt) {
								return o1.seq - o2.seq;
							}
							return o1.cnt - o2.cnt;
						}
					});
					Draw draw = list.remove(0);
					arr.remove(Integer.valueOf(draw.person));
				}
				arr.add(num);
				list.add(new Draw(num, 1, idx++));
			}
		}

		Collections.sort(arr);

		for (int i = 0; i < arr.size(); i++) {
			bw.write(arr.get(i) + " ");
		}
		bw.flush();
	}
}

 

Main

๋ณ€์ˆ˜)
Draw : [ํ•™์ƒ ๋ฒˆํ˜ธ, ์ถ”์ฒœ ์ˆ˜, ๊ฒŒ์‹œ๋œ ์ˆœ์„œ]
n : ์‚ฌ์ง„ํ‹€ ๊ฐœ์ˆ˜
m : ์ถ”์ฒœ ํšŸ์ˆ˜
list : ArrayList <Draw>
arr : ArrayList <์‚ฌ์ง„ํ‹€์— ๊ฒŒ์‹œ๋œ ์‚ฌ์ง„ ๋ฒˆํ˜ธ>
idx : ๊ฒŒ์‹œ๋œ ์ˆœ์„œ

 

์‚ฌ์ง„ํ‹€์˜ ๊ฐœ์ˆ˜(n)์™€ ์ „์ฒด ํ•™์ƒ์˜์ด ์ถ”์ฒœ ํšŸ์ˆ˜(m)๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

 

์ถ”์ฒœ๋ฐ›์€ ํ•™์ƒ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ(num) ๋ฐ›์€ ํ›„ ์‚ฌ์ง„ํ‹€์— ์ด๋ฏธ ๊ฒŒ์‹œ๋˜์–ด ์žˆ๋Š” ์‚ฌ์ง„์ด๋ผ๋ฉด ๊ทธ ์‚ฌ์ง„์˜ ์ถ”์ฒœ ์ˆ˜๋ฅผ ์ฆ๊ฐ€ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์‚ฌ์ง„ํ‹€์— ์—†๋Š” ํ•™์ƒ์˜ ์‚ฌ์ง„์ด๋ผ๋ฉด ์‚ฌ์ง„ํ‹€์ด ๊ฝ‰ ์ฐจ์žˆ๋Š”์ง€๋ฅผ ๋จผ์ € ํ™•์ธํ•œ๋‹ค. ์‚ฌ์ง„ํ‹€์ด ๊ฝ‰ ์ฐจ์žˆ๋‹ค๋ฉด ์‚ฌ์ง„์˜ ์ถ”์ฒœ์ˆ˜๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ, ์ถ”์ฒœ ์ˆ˜๊ฐ€ ๊ฐ™๋‹ค๋ฉด ๊ฒŒ์‹œ๋œ ์ˆœ์„œ๊ฐ€ ์˜ค๋ž˜๋œ ์ˆœ์„œ์ธ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ํ›„ ์ถ”์ฒœ์ˆ˜๊ฐ€ ๋‚ฎ๊ณ , ์˜ค๋ž˜๋œ ์‚ฌ์ง„์„ ์ œ๊ฑฐํ•œ๋‹ค. ๊ทธ ํ›„์— ์ƒˆ๋กœ์šด ์‚ฌ์ง„์„ ๊ฒŒ์‹œํ•œ๋‹ค.

 

์ตœ์ข… ๊ฒŒ์‹œ๋œ ์‚ฌ์ง„์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ ํ›„ ์ถœ๋ ฅํ•œ๋‹ค.