FJ_00460 @ 2022-12-26 14:27:09
为啥只有40分
#include<bits/stdc++.h>
using namespace std;
struct apple{
int high, li;
}app[5005];
int cnt, b, a, n, s;
bool cmp(apple A, apple B)
{
return A.high > B.high;
}
int main()
{
cin >> n >> s >> a >> b;
for(int i = 1 ; i <= n; i++)
cin >> app[i].high >> app[i].li;
sort (app + 1, app + n + 1, cmp);
for(int i = 1 ; i <= n ; i++)
{
if(s <= app[i].li)break;
if(a + b >= app[i].high)
{
cnt ++;
s -= app[i].li;
}
else
break;
}
cout << cnt << endl;
return 0;
}
by telankesi @ 2022-12-26 14:40:26
贪心,应该是按力气从小到大排序,而不是高度
by SmileMask @ 2022-12-26 14:42:34
@water8424
1.该按苹果需要的力气从大到小排序,这样才能拿更多的苹果
2.有些细节问题我帮您改正了
下面是代码
#include<bits/stdc++.h>
using namespace std;
struct apple{
int high, li;
}app[5005];
int cnt, b, a, n, s;
bool cmp(apple A, apple B)
{
return A.li < B.li;
}
int main()
{
cin >> n >> s >> a >> b;
for(int i = 1 ; i <= n; i++)
cin >> app[i].high >> app[i].li;
sort (app + 1, app + n + 1, cmp);
for(int i = 1 ; i <= n ; i++)
{
if(s < app[i].li)break;
if(a + b >= app[i].high)
{
cnt ++;
s -= app[i].li;
}
}
cout << cnt << endl;
return 0;
}
给孩子个关注吧