๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 20044_Project Teams

๋ฟŒ์•ผ._. 2023. 10. 18. 13:58

Silver IV

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

< Project Teams >

 

๋ฌธ์ œ ํ’€์ด 

 

ํ•™์ƒ์˜ ์ฝ”๋”ฉ ์—ญ๋Ÿ‰์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ํ›„ ์—ญ๋Ÿ‰์ด ์ž‘์€ ๊ฒƒ๊ณผ ํฐ ๊ฒƒ์„ ๋”ํ•ด์„œ ํŒ€์„ ๋งŒ๋“ ๋‹ค. ํŒ€์˜ ์ฝ”๋”ฉ ์—ญ๋Ÿ‰ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class _20044_ { // Project Teams

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

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

		int arr[] = new int[n * 2];
		st = new StringTokenizer(bf.readLine());

		for (int i = 0; i < n * 2; i++) {
			arr[i] = Integer.parseInt(st.nextToken());
		}

		Arrays.sort(arr);

		int start = 0, end = 2 * n - 1;

		int result = arr[start++] + arr[end--];
		for (int i = 0; i < n - 1; i++) {
			if (result > arr[start] + arr[end])
				result = arr[start] + arr[end];
			start += 1;
			end -= 1;
		}
		System.out.println(result);
	}
}

 

Main

๋ณ€์ˆ˜)
n : ํŒ€์˜ ์ˆ˜
arr : ํ•™์ƒ์˜ ์ฝ”๋”ฉ ์—ญ๋Ÿ‰ ์ €์žฅ
start, end : index
result : ํŒ€์˜ ์ฝ”๋”ฉ ์—ญ๋Ÿ‰ ํ•ฉ ์ค‘ ์ž‘์€ ๊ฐ’

 

- ํŒ€์˜ ์ˆ˜(n) ์ž…๋ ฅ

- 2n๋ช…์˜ ํ•™์ƒ์˜ ์ฝ”๋”ฉ์—ญ๋Ÿ‰์„ ์ž…๋ ฅ๋ฐ›์•„ arr์— ์ €์žฅ

- arr ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ

- ์ฝ”๋”ฉ ์—ญ๋Ÿ‰์ด ๋‚ฎ์€ ์‚ฌ๋žŒ๊ณผ ๋†’์€ ์‚ฌ๋žŒ์„ ํ•œ ํŒ€์œผ๋กœ ๋งŒ๋“ค์–ด ํ•ฉ์„ ๊ตฌํ•จ. ํŒ€์˜ ์ฝ”๋”ฉ ์—ญ๋Ÿ‰ ํ•ฉ ์ค‘์—์„œ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ฐพ์•„ ์ถœ๋ ฅ