还有两个测试点没过求解!

P1478 陶陶摘苹果(升级版)

无忌 @ 2018-07-05 21:07:54

include<bits/stdc++.h>

using namespace std; struct apple { int height; int strength; }; struct apple app[5001]; bool comp(apple x,apple y) { if(x.strength <=y.strength ) return true; return false; } int main() { int n,s,a,b,ans=0;

cin>>n>>s;
cin>>a>>b;
for(int i=0;i<n;i++)
cin>>app[i].height >>app[i].strength ;
sort(app,app+n,comp);
int k=0;
while(s>=0 && k<n)
{
   if(app[k].height <=a+b) 
    {
    s=s-app[k].strength;
    ans++;
    }
   k++;
}
if(s<0)
cout<<ans-1<<endl;
else cout<<ans<<endl;
return 0;   

}


by ComeOver· @ 2018-07-05 21:25:04

include<bits/stdc++.h>

using namespace std; 
struct apple { 
int height;
int strength; 
}; 
struct apple app[5001]; 
bool comp(apple x,apple y) { if(x.strength <=y.strength ) return true; 
return false; } 
int main() { 
int n,s,a,b,ans=0;

cin>>n>>s;
cin>>a>>b;
for(int i=0;i<n;i++)
cin>>app[i].height >>app[i].strength ;
sort(app,app+n,comp);
int k=0;
while(s>=0 && k<n)
{
   if(app[k].height <=a+b) 
    {
    s=s-app[k].strength;
    ans++;
    }
   k++;
}
if(s<0)
cout<<ans-1<<endl;
else cout<<ans<<endl;
return 0; 

by ComeOver· @ 2018-07-05 21:25:25

应该这么发


by ComeOver· @ 2018-07-05 21:31:05

@无忌 dalao 可不可以写一下注释,不然本蒟蒻看不懂


by ComeOver· @ 2018-07-05 21:37:07

刚刚仔细看了看,大概发现一个问题

你的sort排序排的是高度,而不是力气,如果按你这么写的话,你可能就会把花费力气大的先摘,把可以摘的并且花费力气少的省略掉了,只要sort力气,不要sort高度


by 无忌 @ 2018-07-06 20:20:06

@duboyan 恁看清楚了再说


by ComeOver· @ 2018-07-07 11:21:19

@无忌

对不起,看错了


|