Luogu P1478 陶陶摘苹果

Luogu P1478 陶陶摘苹果(升级版)

题目描述

又是一年秋季时,陶陶家的苹果树结了n个果子。陶陶又跑去摘苹果,这次她有一个a公分的椅子。当他手够不着时,他会站到椅子上再试试。

这次与NOIp2005普及组第一题不同的是:陶陶之前搬凳子,力气只剩下s了。当然,每次摘苹果时都要用一定的力气。陶陶想知道在s<0之前最多能摘到多少个苹果。

现在已知n个苹果到达地上的高度xi,椅子的高度a,陶陶手伸直的最大长度b,陶陶所剩的力气s,陶陶摘一个苹果需要的力气yi,求陶陶最多能摘到多少个苹果。

输入输出格式

输入格式:

第1行:两个数 苹果数n,力气s。

第2行:两个数 椅子的高度a,陶陶手伸直的最大长度b。

第3行~第3+n-1行:每行两个数 苹果高度xi,摘这个苹果需要的力气yi。

输出格式:

只有一个整数,表示陶陶最多能摘到的苹果数。

输入输出样例

输入样例#1:
8 15
20 130
120 3
150 2
110 7
180 1
50 8
200 0
140 3
120 2
输出样例#1:
4

说明

所有数据:n<=5000 a<=50 b<=200 s<=1000 xi<=280 yi<=100

 解法:

 1 /* P1478 陶陶摘苹果(greedy版)
 2  * Au: GG
 3  */
 4 #include <cstdio>
 5 #include <algorithm>
 6 using namespace std;
 7 int n, s, a, b, cnt, y[5000+3], ans;
 8 
 9 int main() {
10     scanf("%d%d%d%d",&n,&s,&a,&b);
11     int xx,yy;
12     for (int i = 1; i <= n; i++){
13         scanf("%d%d",&xx,&yy);
14         xx=xx-a-b; if(xx>0) continue;
15         y[++cnt]=yy;
16     }
17     sort(&y[1],&y[cnt+1]);
18     for(int i=1;i<=cnt;i++){
19         s-=y[i];
20         if(s>=0) ans++;
21         else break;
22     }
23     printf("%d
", ans);
24     return 0;
25 }

Post author 作者: Grey
Copyright Notice 版权说明: Except where otherwise noted, all content of this blog is licensed under a CC BY-NC-SA 4.0 International license. 除非另有说明,本博客上的所有文章均受 知识共享署名 - 非商业性使用 - 相同方式共享 4.0 国际许可协议 保护。
原文地址:https://www.cnblogs.com/greyqz/p/6658658.html