๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1083_์†ŒํŠธ

๋ฟŒ์•ผ._. 2024. 3. 19. 21:30

Gold V

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

< ์†ŒํŠธ >

 

๋ฌธ์ œ ํ’€์ด 

 

๊ฐ€์žฅ ํฐ ๊ฐ’๋ถ€ํ„ฐ ์•ž์œผ๋กœ ์˜ฌ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ฉฐ ๊ฐ€๋Šฅํ•˜๋ฉด ์•ž์œผ๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.

 

 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 _1083_ { // ์†ŒํŠธ
	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());

		st = new StringTokenizer(bf.readLine());
		ArrayList<int[]> list = new ArrayList<>();

		ArrayList<Integer> arr = new ArrayList<>();
		for (int i = 0; i < n; i++) {
			arr.add(Integer.parseInt(st.nextToken()));
			list.add(new int[] { i, arr.get(i) });
		}

		int s = Integer.parseInt(bf.readLine());

		Collections.sort(list, new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				if (o1[1] == o2[1])
					return o1[0] - o2[0];
				return o2[1] - o1[1];
			}
		});

		int idx = 0;

		while (s > 0) {
			for (int i = 0; i < list.size(); i++) {
				int info[] = list.get(i);
				if (info[1] > arr.get(idx) && info[0] - idx <= s) {
					s -= (info[0] - idx);
					arr.remove(info[0]);
					arr.add(idx, info[1]);
					break;
				}
			}
			list.clear();

			idx += 1;

			if (idx == n) {
				break;
			}

			for (int i = idx; i < n; i++) {
				list.add(new int[] { i, arr.get(i) });
			}

			Collections.sort(list, new Comparator<int[]>() {
				@Override
				public int compare(int[] o1, int[] o2) {
					if (o1[1] == o2[1])
						return o1[0] - o2[0];
					return o2[1] - o1[1];
				}
			});
		}

		for (int i = 0; i < n; i++) {
			bw.write(arr.get(i) + " ");
		}
		bw.flush();
	}
}
๋ณ€์ˆ˜)
n : ๋ฐฐ์—ด ํฌ๊ธฐ
list : [index, ๋ฐฐ์—ด ๊ฐ’]
arr : ๋ฐฐ์—ด
s : ์ตœ๋Œ€ ๊ตํ™˜ ํšŸ์ˆ˜
idx : ๋ฐฐ์—ด ๋ฐ”๊ฟ€ ์œ„์น˜

 

๋ฐฐ์—ด ํฌ๊ธฐ๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ๋ฐฐ์—ด ํฌ๊ธฐ๋งŒํผ ๋ฐฐ์—ด ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์•„ arr์— ์ €์žฅํ•œ๋‹ค. ์†ŒํŠธํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์‚ฌ์ „์ˆœ์œผ๋กœ ๊ฐ€์žฅ ๋’ท์„œ๋Š” ๊ฒƒ์„ ์ถœ๋ ฅํ•˜๋ ค๋ฉด ํฐ ๊ฐ’์ด ์•ž์— ์™€์•ผ ํ•œ๋‹ค. ArrayList์— [index, ๋ฐฐ์—ด ๊ฐ’]์„ ์ €์žฅํ•œ ํ›„ ๋ฐฐ์—ด ๊ฐ’์ด ํฐ ์ˆœ์„œ๋Œ€๋กœ, ๋ฐฐ์—ด ๊ฐ’์ด ๊ฐ™๋‹ค๋ฉด index๊ฐ€ ์ž‘์€ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•œ๋‹ค. ๋ฐฐ์—ด 0๋ฒˆ์งธ ๊ฐ’๋ถ€ํ„ฐ ํฐ ๊ฐ’์œผ๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด idx๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๋ฉฐ s๊ฐ€ 0๋ณด๋‹ค ํด ๋•Œ ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค.

 

1) ์ •๋ ฌํ•œ list๋ฅผ ์ˆœํ™˜ํ•˜๋ฉฐ ํ˜„์žฌ idx์œ„์น˜์— ์žˆ๋Š” ๋ฐฐ์—ด ๊ฐ’ ๋ณด๋‹ค ํฌ๊ณ  ๊ตํ™˜ ํšŸ์ˆ˜๊ฐ€ ๋‚จ์•„์žˆ๋‹ค๋ฉด ๊ตํ™˜ ํšŸ์ˆ˜๋ฅผ ์ฐจ๊ฐํ•˜๊ณ  ๊ฐ’์„ idx์œ„์น˜๋กœ ๊ฐ€์ ธ์˜จ ํ›„ ์ˆœํ™˜์„ ์ข…๋ฃŒํ•œ๋‹ค.

2) list๋ฅผ ๋น„์šฐ๊ณ  ๋‹ค์Œ ์œ„์น˜์˜ ๋ฐฐ์—ด ๊ฐ’์„ ๊ตํ•œํ•ด์•ผ ํ•˜๋ฏ€๋กœ idx+1์„ ํ•œ๋‹ค.

3) idx๊ฐ€ n๊ณผ ๊ฐ™๋‹ค๋ฉด ๋” ์ด์ƒ ๊ตํ™˜ํ•  ๊ฐ’์ด ์—†์œผ๋ฏ€๋กœ ์ข…๋ฃŒํ•œ๋‹ค.

4) ๋ฐฐ์—ด์˜ idx๋ฒˆ์งธ ๊ฐ’๋ถ€ํ„ฐ ๋๊นŒ์ง€ list์— [index, ๋ฐฐ์—ด ๊ฐ’]์„ ์ €์žฅํ•œ ํ›„ ์œ„์—์„œ์˜ ์ •๋ ฌ ์กฐ๊ฑด๊ณผ ๋˜‘๊ฐ™์ด ์ •๋ ฌํ•œ๋‹ค.

 

๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค.