求大佬help

P1478 陶陶摘苹果(升级版)

swan999 @ 2023-12-19 17:33:06

代码求调 第一个样例不知道为啥子过不了(80)

import java.io.BufferedInputStream;
import java.util.*;

class apple{
    int x; // x表示高度
    int y; // y 表示力气
    public apple(int x,int y){
        this.x = x;
        this.y = y;
    }
}
public class Main {
    static int n,s;
    static int a,b;
    public static void main(String[] args){
        Scanner sc = new Scanner(new BufferedInputStream(System.in));
        n = sc.nextInt();
        s = sc.nextInt();
        a = sc.nextInt();
        b = sc.nextInt();
        List<apple> list = new ArrayList<>();
        for(int i = 0;i < n;i ++){
            int x = sc.nextInt();
            int y = sc.nextInt();
            list.add(new apple(x,y));
        }
        Comparator<apple> comparator = new Comparator<apple>() {
            @Override
            public int compare(apple o1, apple o2) {
                return o1.y - o2.y;
            }
        };
        Collections.sort(list,comparator);
        // 找出答案
        int max = a + b;
        int res = 0;
        for(int i = 0;i < list.size();i ++){
            if(list.get(i).x > max)
                continue;
            if(s < 0)
                continue;
            if(s > list.get(i).y){
                res ++;
                s -= list.get(i).y;
            }
        }
        System.out.println(res);
    }
}

by swan999 @ 2023-12-19 18:39:03

孺弱 已A 感谢所有想要提供帮助的大佬


|