알고리즘

백준 6159번 코스튬 파티 (JAVA)

박카스마시며코딩 2023. 1. 19. 14:48

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);
    }
}