Aural_Psynapse @ 2019-08-12 19:20:27
#include<bits/stdc++.h>
using namespace std;
int appleheight[10001],needeffort[10001];
int chair,pick=0,height,taotaoeffort,apple,i,j,k;
int main()
{
cin>>apple>>taotaoeffort;
cin>>chair>>height;
for(i=1;i<=apple;i++)
{
cin>>appleheight[i]>>needeffort[i];
}
for(i=1;i<apple;i++)
for(j=i+1;j<=apple;j++)
if(needeffort[i]>needeffort[i+1])
{
swap(needeffort[i],needeffort[i+1]);
swap(appleheight[i],appleheight[i+1]);
}
for(i=1;i<=apple;i++)
{
if(chair+height<=appleheight[i]&&taotaoeffort>=needeffort[i])
{
pick++;
taotaoeffort-=needeffort[i];
}
}
cout<<pick;
return 0;
}
排序用了冒泡,不知道怎么回事 不会贪心,用简单的判断
by 闲鱼 @ 2019-08-12 19:28:56
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAX 5005
struct apple{
int x;
int y;
}arr[MAX];
bool cmp(apple an,apple bn){
return an.y<bn.y;
}
int main()
{
int n,s,a,b,xx,yy,ans=0,can=0;
scanf("%d%d%d%d",&n,&s,&a,&b);
for(int i=1;i<=n;i++){
scanf("%d%d",&xx,&yy);
if(a+b>=xx){
arr[++can].x=xx;
arr[can].y=yy;
}
}
sort(arr+1,arr+can+1,cmp);
for(int i=1;s>=arr[i].y && i<=can;i++){
ans++;
s-=arr[i].y;
}
printf("%d\n",ans);
return 0;
}
by 闲鱼 @ 2019-08-12 19:36:50
在陶陶站在板凳上能够被摘到的苹果中选择花费力气最小的苹果优先摘(贪心策略),即可保证程序计算得到能摘的苹果数量最大。
具体实现是先将每一个苹果的信息输入,判断陶陶站在板凳上是否能摘到苹果,是则将这个苹果的信息保存在arr数组中,sort根据摘每个苹果所花费的力气从小到大排序,再循环遍历至陶陶的力气用完为止(每次循环ans++)
by Aural_Psynapse @ 2019-08-12 19:43:28
@hao_cheng 谢谢~~dalao~~大佬