https://www.acmicpc.net/problem/6159
6159번: 코스튬 파티
한 농부가 할로윈 파티에 그의 소들을 데려가려고한다. 아쉽게도 농부에게는 코스튬이 한벌밖에 없다. 그 코스튬에는 정확하게 사이즈는 S(1 <= S <= 1,000,000)이며, 최대 소 두마리가 들어간다. 농
www.acmicpc.net
package BOJ.twopoint;
import java.awt.print.Pageable;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.function.Function;
public class BOJ_6159 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
Function<String,Integer> stoi = Integer::parseInt;
int n = stoi.apply(st.nextToken());
int s = stoi.apply(st.nextToken());
int[] arr = new int[n];
for(int i = 0 ; i < n ; i++){
arr[i] = stoi.apply(br.readLine());
}
Arrays.sort(arr);
int startIndex = 0;
int endIndex = n-1;
int result = 0;
while(startIndex < endIndex){
int sum = arr[startIndex] + arr[endIndex];
if(sum <= s){
result += endIndex - startIndex;
}
if(sum <= s){
startIndex++;
}
if(sum > s){
endIndex--;
}
}
System.out.println(result);
}
}
'알고리즘' 카테고리의 다른 글
백준 13702번 이상한 술집 (JAVA) (0) | 2023.01.20 |
---|---|
백준 k보다 큰 구간 (JAVA) (0) | 2023.01.19 |
백준 11581번 구호물자(JAVA) (0) | 2023.01.17 |
백준 15558번 점프 게임 (JAVA) (0) | 2023.01.16 |
프로그래머스 귤 고르기(JAVA) (0) | 2023.01.15 |