• 문제 링크
6159번: Costume Party
It's Halloween! Farmer John is taking the cows to a costume party, but unfortunately he only has one costume. The costume fits precisely two cows with a length of S (1 <= S <= 1,000,000). FJ has N cows (2 <= N <= 20,000) conveniently numbered 1..N; cow i h
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read(), s = read();
int[] arr = new int[n];
while (n-- > 0) arr[n] = read();
Arrays.sort(arr);
int cnt = 0, i = 0, j = arr.length - 1;
while (i < j) {
if (arr[i] + arr[j] > s) j--;
else cnt += j - i++;
}
bw.write(String.valueOf(cnt));
bw.flush();
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 5089 Travelling Salesman - Data Structure / Java (0) | 2023.09.08 |
---|---|
[백준] 15098 No Duplicates - Data Structure / Java (0) | 2023.09.07 |
[백준] 15721 번데기 - Brute Force / Java (0) | 2023.09.05 |
[백준] 1895 필터 - Brute Force / Java (0) | 2023.09.04 |
[백준] 2993 세 부분 - Brute Force / Java (0) | 2023.09.03 |
댓글