暑期dp46道(6)抢劫Robberies ——HDOJ 2955
来源:互联网 发布:埃米特矩阵 编辑:程序博客网 时间:2024/05/17 04:33
<span style="font-family: Arial, Helvetica, sans-serif; font-size: 18px; background-color: rgb(255, 255, 255);">题目链接:<span style="color: rgb(51, 102, 255);">http://acm.hdu.edu.cn/showproblem.php?pid=2955</span><span style="color: rgb(255, 0, 0);">(01背包)</span></span>
思路:刚开始想裸背包,求在安全概率内的最大金钱数,但是这题的浮点位数不能确定,而且概率不是相加的,所以就应该反过来想,求获得i金钱的最大安全概率,
然后枚举,记录满足条件的最大i值.
w[i]表示第i个银行能抢到的金额,c[i]表示在第i个银行抢劫的安全概率(各银行间相互独立)
ans[j]表示获取数量为j的金钱的最大安全概率,剩下的就是01背包,
ans[j]=Max(ans[j],ans[j-c[i]]*w[i])//注意是“*”
要注意安全概率初始化,这个没什么说的
PS:01背包是逆向枚举
附上代码:
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<queue>#include<stack>#include<cmath>#include<limits>#include<string>using namespace std;#define LL long long#define debug 0const int maxn=100+5;int w[maxn];double ans[10010],c[maxn];int caseNum,n,v;double total;void Do(){ //printf("%d\n",n); for(int i=1; i<=n; i++) for(int j=v; j>=w[i]; j--) { if(ans[j]<ans[j-w[i]]*c[i]) ans[j]=ans[j-w[i]]*c[i]; } //printf("%d\n",n); for(int i=v; i>=0; i--) { if(ans[i]>total) { printf("%d\n",i); break; } }}int main(){#if debug freopen("in.txt","r",stdin);#endif // debug scanf("%d",&caseNum); while(caseNum--) { //printf("%d\n",caseNum); //memset(ans,0,sizeof(ans)); //ans[0]=1; v=0; scanf("%lf %d",&total,&n); total=1-total; for(int i=1; i<=n; i++) { scanf("%d %lf",&w[i],&c[i]); v+=w[i];//记录最大金额,用于后来的背包计算 c[i]=1-c[i]; } for(int i=1; i<=v; i++) { ans[i]=0; } ans[0]=1; Do(); } return 0;}
0 0
- 暑期dp46道(6)抢劫Robberies ——HDOJ 2955
- 暑期dp46道(11)——HDOJ 1422重温世界杯 dp+记忆化搜索
- 暑期dp46道(27)--HDOJ 3008 Warcraft
- 暑期dp46道(36)--HDOJ 2577
- 暑期dp46道(37) HDOJ 2844 COINS
- 暑期dp46道(44)--HDOJ 1058
- 暑期dp46道(46)--HDOJ 1069
- 暑期dp46道(13)——HDOJ 1160FatMouse's Speed 排序+最长严格下降子序列+回溯
- 暑期dp46道(5)hdoj 1421 搬寝室s
- 暑期dp46道(10)HDOJ 2602 Bone Collect 01背包
- 暑期dp46道(21)HDOJ 2059 龟兔赛跑
- 暑期dp46道(22)--HDOJ 1158 Employment Planning
- 暑期dp46道(25)--HDOJ 1300 Pearls
- 暑期dp46道(30) HDOJ 1227 Fast Food
- 暑期dp46道(33)--HDOJ 1176 免费馅饼 类树塔:
- 暑期dp46道(34)--HDOJ 1203 01背包
- 暑期dp46道(35)--HDOJ 2159 FATE 背包问题
- 暑期dp46道(39)--HDOJ 2870 最大子矩阵面积...
- [LeetCode] 69. Sqrt(x)
- 【数组13】数组中重复的数字
- 与服务器相关联的知识点
- 【POJ 1611】The Suspects并查集
- nginx的用户手册
- 暑期dp46道(6)抢劫Robberies ——HDOJ 2955
- The source files...are both configured to produce the output file,The project cannot be built.
- liunx下配置yocto工程环境
- 网易2016笔试(3)
- 杭电-1878 欧拉回路
- HDU 3791 二叉搜索树(数组实现)
- yield
- <sdut-ACM> 3470数学黑洞
- NOIP2015普及组T4salesman题解