• 문제 링크
28353번: 고양이 카페
첫째 줄에 정수 $N$과 $K$가 공백으로 구분되어 주어진다. $(1 \leq N \leq 5\,000;$ $1 \leq K \leq 10^9)$ 둘째 줄에는 각 고양이의 무게를 의미하는 $N$개의 정수 $w_1, w_2, \dotsm, w_N$이 공백으로 구분되어 주어
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(), k = 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] > k) j--;
else {
i++;
j--;
cnt++;
}
}
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' 카테고리의 다른 글
[백준] 17216 가장 큰 감소 부분 수열 - Dynamic Programming / Java (0) | 2023.09.25 |
---|---|
[백준] 28464 Potato - Greedy / Java (0) | 2023.09.24 |
[백준] 25707 팔찌 만들기 - Greedy / Java (0) | 2023.09.22 |
[백준] 29615 알파빌과 베타빌 - Greedy / Java (0) | 2023.09.21 |
[백준] 28324 스케이트 연습 - Greedy / Java (0) | 2023.09.20 |
댓글