又是一年秋季时,陶陶家的苹果树结了 n 个果子。陶陶又跑去摘苹果,这次他有一个 a 公分的椅子。当他手够不着时,他会站到椅子上再试试。
陶陶之前搬凳子,力气只剩下 s 了。当然,每次摘苹果时都要用一定的力气。陶陶想知道在 s<0 之前最多能摘到多少个苹果。
现在已知有 n 个苹果,陶陶所剩的力气 s,椅子的高度 a,陶陶手伸直的最大长度 b,每个苹果距离地面的高度xi,陶陶摘一个苹果需要的力气 yi,求陶陶最多能摘到多少个苹果。
第 1 行:两个数 苹果数 n,力气 s。
第 2 行:两个数 椅子的高度 a,陶陶手伸直的最大长度 b。
第 3 行~第 3+n−1 行:每行两个数 苹果高度 xi,摘这个苹果需要的力气 yi。
只有一个整数,表示陶陶最多能摘到的苹果数。
8 15
20 130
120 3
150 2
110 7
180 1
50 8
200 0
140 3
120 2
4
对于 100% 的数据,n≤5000, a≤50, b≤200, s≤1000, xi≤280, yi≤100。
我的答案:
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>
#include <map>
#include <stack>
#include <algorithm>
#include <string>
#include <queue>
#include <deque>
#include <vector>
using namespace std;
struct tree{
int x,y;
bool operator < (const tree &o) const{
return y<o.y;
}
};
int A[1145140],B[1145140];
int main() {
tree tre;
int n,s,a,b,cnt=0;
cin>>n>>s>>b>>a;
for(int i=0;i<n;i++){
cin>>tre.x>>tre.y;
A[i]=tre.x,B[i]=tre.y;
}
sort(A,A+n);sort(B,B+n);
for(int i=0;i<1145141919810;i++){
if(A[i]<=a+b && B[i]<=s){
s-=B[i];
cnt++;
}
else {
cout<<cnt;
return 0;
}
}
}