๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/11597)
< Excellence >
๋ฌธ์ ํ์ด
ํ์ ๊ตฌ์ฑํ ์ ์๋ ํ์ ์ ์ต๋๊ฐ์ ๊ตฌํ๊ธฐ ์ํด์๋ ์ ๋ ฅ๋ฐ์ ํ์ ์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ์ฌ ์์ ๊ฐ๊ณผ ํฐ ๊ฐ์ ๋ํ๋ค.
๋ง์ฝ ์ ๋ ฅ๊ฐ์ด ๋ค์๊ณผ ๊ฐ๋ค๋ฉด
4
1
2
3
5
๋จผ์ ํ์ ์ ์ ๋ ฌํ๋ค.
1 2 3 5
1+5 = 6
2+3 = 5
์ด๋ฏ๋ก X์ ์ต๋๊ฐ์ 5๊ฐ ๋๋ค.
my solution (Java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class _11597_ { // Excellence
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(bf.readLine());
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(bf.readLine());
}
Arrays.sort(arr);
int result = Integer.MAX_VALUE;
for (int i = 0; i < n / 2; i++) {
result = Math.min(result, arr[i] + arr[n - 1 - i]);
}
System.out.println(result);
}
}
๋ณ์)
n : ํ์ ์
arr : ํ์ ์ ์ฅ
result : ํ์ ๊ตฌ์ฑํ ์ ์๋ ํ์ ์ ์ต๋๊ฐ
ํ์ ์ n์ ์ ๋ ฅ๋ฐ๋๋ค. n๋งํผ ํ์ ์ ์ ๋ ฅ๋ฐ์ arr์ ์ ์ฅ ํ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ค. arr์ ์์ฐจ ํ์ํ๋ฉฐ ์๊ณผ ๋ค๋ฅผ ๋ํด result์ ์ต์๊ฐ์ ์ฐพ๋๋ค.
์ต์ข ํ์ ๊ตฌ์ฑํ ์ ์๋ ํ์ ์ ์ต๋๊ฐ์ธ result๋ฅผ ์ถ๋ ฅํ๋ค.
'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 23895_Allocation (0) | 2025.03.26 |
---|---|
[Baekjoon] 6566_์ ๋๊ทธ๋จ ๊ทธ๋ฃน (0) | 2025.03.25 |
[Baekjoon] 6123_O Those Fads (0) | 2025.03.21 |
[Baekjoon] 12596_Odd Man Out (Large) (0) | 2025.03.20 |
[Baekjoon] 26975_Cow College (0) | 2025.03.18 |