๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 11968_High Card Wins

๋ฟŒ์•ผ._. 2024. 10. 25. 22:13
๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/11968)

< High Card Wins >

 

๋ฌธ์ œ ํ’€์ด 

 

๊ฐ ํ”Œ๋ ˆ์ด์–ด๊ฐ€ ๊ฐ€์ง„ ์นด๋“œ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค. ์นด๋“œ ๋ฐฐ์—ด์„ ๋’ค์—์„œ๋ถ€ํ„ฐ(=ํฐ ๊ฐ’๋ถ€ํ„ฐ) ๋น„๊ตํ•˜๋ฉด์„œ ์ƒ๋Œ€๋ฐฉ ์นด๋“œ ๊ฐ’๋ณด๋‹ค ํฌ๋ฉด ์ ์ˆ˜ ํš๋“์„, ํฌ์ง€ ์•Š๋‹ค๋ฉด ๋งจ ์•ž์˜ ์นด๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค.

 

= ๋‚ด๊ฐ€ ๊ฐ€์ง„ ์นด๋“œ ํฐ ๊ฐ’์„ ์ƒ๋Œ€๋ฐฉ์˜ ์นด๋“œ ์ค‘ ํฐ ๊ฐ’๊ณผ ๋น„๊ตํ•ด์„œ ์ด๊ธฐ๋„๋ก ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ์ตœ๋Œ“๊ฐ’์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class _11968_ { // High Card Wins
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(bf.readLine());
		int a[] = new int[N];
		int b[] = new int[N];
		for (int i = 0; i < N; i++) {
			a[i] = Integer.parseInt(bf.readLine());
		}
		Arrays.sort(a);
		int num = 1, idxA = 0, idxB = 0;
		while (num <= 2 * N) {
			if (idxA<N && num == a[idxA]) {
				num += 1;
				idxA += 1;
			} else {
				b[idxB++] = num++;
			}
		}
		idxA = N - 1;
		idxB = N - 1;
		int result = 0;
		while (idxA >= 0) {
			if (a[idxA] < b[idxB]) {
				idxB -= 1;
				result += 1;
			}
			idxA -= 1;
		}
		System.out.println(result);
	}
}
๋ณ€์ˆ˜)
N : 1์ธ๋‹น ์นด๋“œ ๊ฐœ์ˆ˜
a, b : ๊ฐ ํ”Œ๋ ˆ์ด์–ด์˜ ์นด๋“œ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด

 

์นด๋“œ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค. ์นด๋“œ ๊ฐœ์ˆ˜๋งŒํผ ์ƒ๋Œ€๋ฐฉ์˜ ์นด๋“œ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ a ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค. a ๋ฐฐ์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ํ›„ a ๋ฐฐ์—ด์—์„œ ์—†๋Š” ๊ฐ’์„ b ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค. a ๋ฐฐ์—ด์„ ์ „์ฒด ํƒ์ƒ‰ํ•  ๋•Œ๊นŒ์ง€ ๋‹ค์Œ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•œ๋‹ค. a ๋ฐฐ์—ด๊ณผ b ๋ฐฐ์—ด์„ ๋’ค์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฉฐ b ๋ฐฐ์—ด์˜ ๊ฐ’์ด ํฌ๋‹ค๋ฉด ์ ์ˆ˜ ํš๋“๊ณผ b ์ธ๋ฑ์Šค ๊ฐ์†Œ ๋ฐ a ์ธ๋ฑ์Šค ๊ฐ์†Œ๋ฅผ, b ๋ฐฐ์—ด์˜ ๊ฐ’์ด ํฌ์ง€ ์•Š๋‹ค๋ฉด a ์ธ๋ฑ์Šค๋ฅผ ๊ฐ์†Œ์‹œํ‚จ๋‹ค.

 

์ตœ์ข… result๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.



 

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

[Baekjoon] 6160_Election Time  (0) 2024.10.29
[Baekjoon] 10527_Judging Troubles  (0) 2024.10.28
[Baekjoon] 7596_MP3 Songs  (0) 2024.10.24
[Baekjoon] 9414_ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๋Œ€ํšŒ ์ „์šฉ ๋ถ€์ง€  (0) 2024.10.23
[Baekjoon] 5939_Race Results  (0) 2024.10.22