题目链接:陶陶摘苹果(升级版)
java代码:
import java.util.Arrays;
import java.util.Scanner;
class Node implements Comparable<Node>{
int x,y;
public Node(int x, int y) {
super();
this.x = x;
this.y = y;
}
// 按摘每个苹果需要的力气升序排列
public int compareTo(Node newNode) {
return Integer.compare(this.y, newNode.y);
}
}
public class Main {
public static void main(String[] args) {
int n,s,height,count=0;
Scanner in = new Scanner(System.in);
n = in.nextInt();
s = in.nextInt();
height = in.nextInt()+in.nextInt();
Node arr[] = new Node[n];
for(int i=0;i<n;++i)
arr[i] = new Node(in.nextInt(),in.nextInt());
Arrays.sort(arr);
for(int i=0;i<n;++i) {
// 剩余的力气要够用,且苹果的高度不能高于陶陶站在椅子上的最大高度
if(s>=arr[i].y&&height>=arr[i].x) {
count++;
s -= arr[i].y; //更新剩余的力气
}
}
System.out.println(count);
}
}