贪心陷阱
引例:
洛谷看题
in.txt
5 5
0 9 9 9 9
8 8 8 8 8
9 9 9 9 9
3 3 3 9 9
100 100 8 9 0
如果用贪心(当然不对):
0-9-9-9-9-8-9-9-0-9-9-9-9-9-9-8-0
out.txt
124
动归:
0-8-9-3-100-100-8-9-0-9-9-9-9-9-9-8-0
out.txt
299
显然贪心是错的
为毛呢?
贪心只顾眼前\
贪心算法需要证明其正确性
贪心算法需要证明其正确性
这便是贪心陷阱(自编名词)
那如何规避贪心陷阱呢?
证明可行性,简单来讲,就是证明再此只顾眼前是正确的
替换法是个好东西
洛谷看题
这题。。。不可以贪心吧?
本文已结束,吗?
请证明上面题用贪心的可行性
上题ac代码(贪心)
懒得自己写系列
by 洛谷网友
献给打不开的朋友
#include<cstdio>
#include<algorithm>//用到sort
using namespace std;
struct Node{//金币结构体
int w,v;//w表示重量,v表示价值
}a[110];
int read(){//普通的快读,不解释
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
bool cmp(Node aa,Node bb){//定义排序方法
return aa.v*bb.w>aa.w*bb.v;//按性价比从高到低排序,为防止精度问题直接交叉相乘
}
int main(){//主函数
int n=read(),m=read();
double ans=0;//记录答案
for(int i=1;i<=n;i++) a[i].w=read(),a[i].v=read();
sort(a+1,a+n+1,cmp);//排序
for(int i=1;i<=n;i++){//一次遍历
if(a[i].w<=m) ans+=a[i].v,m-=a[i].w;//够就全拿
else{//不够
ans+=a[i].v*m*1.0/(a[i].w*1.0);//拿上能拿的部分,注意强转double
break;//直接退出循环
}
}
printf("%.2lf",ans);//保留2位小数
return 0;//华丽结束
}
感谢关注(手动滑稽)