20分求助

P1478 陶陶摘苹果(升级版)

半天没看懂…… ```cpp #include <bits/stdc++.h> using namespace std; struct st{ long long height,each_energy; }; st s[5005]; bool cmp(st x,st y) {return x.each_energy<y.each_energy;} long long n,ch,b,energy,cnt=1,sum=0; int main(){ // freopen("picking.in","r",stdin); // freopen("picking.out","w",stdout); cin >> n >> energy >> ch >> b; if(n==0) {cout<<0;return 0;} for(long long i=1;i<=n;++i){ cin >> s[i].height; cin >> s[i].each_energy; } sort(s+1,s+n+1,cmp); while(energy>=0){ if(s[cnt].height<=(ch+b)){ energy-=s[cnt].each_energy; sum++; } cnt++; } cout << sum-1; } ``` AC
by CHWe668 @ 2023-10-20 18:57:41


写一个sort排序就可以了
by CHWe668 @ 2023-10-20 18:58:35


自己看 ```cpp #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <stack> #include <queue> #include <cctype> #include <iomanip> using namespace std; struct Apple{ int x; int y; }; int n, s, a, b, cnt; Apple arr[5005]; bool cmp(Apple a, Apple b){ if(a.y != b.y){ return a.y < b.y; } return a.x < b.x; } int main(){ cin>>n>>s>>a>>b; for(int i=1; i<=n; i++){ cin>>arr[i].x>>arr[i].y; } sort(arr+1, arr+n+1, cmp); for(int i=1; i<=n && s; i++){ if(s>=arr[i].y && a+b>=arr[i].x){ cnt++; s -= arr[i].y; } } cout<<cnt; return 0; } ```
by tiansuo1145 @ 2023-10-21 21:23:03


|