P1478 陶陶摘苹果(升级版)java 洛谷
一.题目描述:
题目描述
又是一年秋季时,陶陶家的苹果树结了 nn 个果子。陶陶又跑去摘苹果,这次他有一个 aa 公分的椅子。当他手够不着时,他会站到椅子上再试试。
这次与 NOIp2005 普及组第一题不同的是:陶陶之前搬凳子,力气只剩下 ss 了。当然,每次摘苹果时都要用一定的力气。陶陶想知道在 s<0s<0 之前最多能摘到多少个苹果。
现在已知 nn 个苹果到达地上的高度 x_ix
i
,椅子的高度 aa,陶陶手伸直的最大长度 bb,陶陶所剩的力气 ss,陶陶摘一个苹果需要的力气 y_iy
i
,求陶陶最多能摘到多少个苹果。
输入格式
第 11 行:两个数 苹果数 nn,力气 ss。
第 22 行:两个数 椅子的高度 aa,陶陶手伸直的最大长度 bb。
第 33 行~第 3+n-13+n−1 行:每行两个数 苹果高度 x_ix
i
,摘这个苹果需要的力气 y_iy
i
。
输出格式
只有一个整数,表示陶陶最多能摘到的苹果数。
输入输出样例
输入 #1复制
8 15
20 130
120 3
150 2
110 7
180 1
50 8
200 0
140 3
120 2
输出 #1复制
4
说明/提示
对于 100%100% 的数据,n\leq 5000n≤5000, a\leq 50a≤50, b\leq 200b≤200, s\leq 1000s≤1000, x_i\leq 280x
i
≤280, y_i\leq 100y
i
≤100。
二.代码实现:
```java
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int s=sc.nextInt();
int a= sc.nextInt();
int b=sc.nextInt();
int apple[][]=new int[n][2];
for(int i=0;i<n;i++)
{
for(int j=0;j<2;j++)
{
apple[i][j]=sc.nextInt();
}
}
int temp;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(apple[j][1]>apple[j+1][1])
{
temp=apple[j][0];
apple[j][0]=apple[j+1][0];
apple[j+1][0]=temp;
temp=apple[j][1];
apple[j][1]=apple[j+1][1];
apple[j+1][1]=temp;
}
}
}
int sum=0;
int i=0;
while(s>=0&&i<n)
{
if(apple[i][1]<=s)
{
if(apple[i][0]<=a+b)
{
s=s-apple[i][1];
sum++;
}
}
i++;
}
System.out.println(sum);
}
}