HDU 2955 Robberies(01背包变形)
Robberies
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16522 Accepted Submission(s): 6065
aspiring Roy the Robber has seen a lot of American movies, and knows
that the bad guys usually gets caught in the end, often because they
become too greedy. He has decided to work in the lucrative business of
bank robbery only for a short while, before retiring to a comfortable
job at a university.
For
a few months now, Roy has been assessing the security of various banks
and the amount of cash they hold. He wants to make a calculated risk,
and grab as much money as possible.
His mother, Ola, has
decided upon a tolerable probability of getting caught. She feels that
he is safe enough if the banks he robs together give a probability less
than this.
first line of input gives T, the number of cases. For each scenario,
the first line of input gives a floating point number P, the probability
Roy needs to be below, and an integer N, the number of banks he has
plans for. Then follow N lines, where line j gives an integer Mj and a
floating point number Pj .
Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj .
each test case, output a line with the maximum number of millions he
can expect to get while the probability of getting caught is less than
the limit set.
Notes and Constraints
0 < T <= 100
0.0 <= P <= 1.0
0 < N <= 100
0 < Mj <= 100
0.0 <= Pj <= 1.0
A bank goes bankrupt if it is robbed, and you may assume that all
probabilities are independent as the police have very low funds.
0.04 3
1 0.02
2 0.03
3 0.05
0.06 3
2 0.03
2 0.03
3 0.05
0.10 3
1 0.03
2 0.02
3 0.05
4
6
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 12345
#define M 12 float f[N],p[N],P;
int m[N],n; int main()
{
// freopen("1.txt", "r", stdin);
// freopen("2.txt", "w", stdout);
int T;cin>>T;
while(T--)
{
int sum=;
scanf("%f%d",&P,&n);
for(int i=;i<=n;i++)
{
scanf("%d%f",&m[i],&p[i]);
sum+=m[i];
}
memset(f,,sizeof(f));//初始化
f[]=1.0;//边界,抢劫0钱不被抓的概率是1 for(int i=;i<=n;i++)
{
for(int j=sum;j>=m[i];j--)
{
f[j]=max(f[j], f[j-m[i]]*(-p[i]) );//独立事件相乘
}
} for(int i=sum;i>=;i--)
{
// printf("%f ",f[i]);
if(f[i]>=(-P))//找到第一不被抓概率大于等于(1-P)的i就是答案
{
printf("%d\n",i);
break;
}
}
}
return ;
}
2)
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 12345 float f[N],p[N],P;
int m[N],n; int main()
{
int T;cin>>T;
while(T--)
{
int sum=;
scanf("%f%d",&P,&n);
for(int i=;i<=n;i++)
{
scanf("%d%f",&m[i],&p[i]);
sum+=m[i];
} for(int i=;i<N-;i++)//初始化
f[i]=1.0;
f[]=0.0;//边界,抢劫0钱的被抓概率是0 for(int i=;i<=n;i++)
{
for(int j=sum;j>=m[i];j--)
{
f[j]=min(f[j], -(-f[j-m[i]])*(-p[i]) );//概率知识,独立事件,对立事件
}
} for(int i=sum;i>=;i--)
{
// printf("%f ",f[i]);
if(f[i]<=P)//找到第一个概率小于等于安全概率P的i,就是答案
{
printf("%d\n",i);
break;
}
}
}
return ;
}
HDU 2955 Robberies(01背包变形)的更多相关文章
- HDU 2955 Robberies --01背包变形
这题有些巧妙,看了别人的题解才知道做的. 因为按常规思路的话,背包容量为浮点数,,不好存储,且不能直接相加,所以换一种思路,将背包容量与价值互换,即令各银行总值为背包容量,逃跑概率(1-P)为价值,即 ...
- hdu 2955 Robberies (01背包)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 思路:一开始看急了,以为概率是直接相加的,wa了无数发,这道题目给的是被抓的概率,我们应该先求出总的 ...
- hdu 2955 Robberies 0-1背包/概率初始化
/*Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU——2955 Robberies (0-1背包)
题意:有N个银行,每抢一个银行,可以获得\(v_i\)的前,但是会有\(p_i\)的概率被抓.现在要把被抓概率控制在\(P\)之下,求最多能抢到多少钱. 分析:0-1背包的变形,把重量变成了概率,因为 ...
- hdu 2955 Robberies (01背包好题)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- HDU 2955 Robberies(01背包)
Robberies Problem Description The aspiring Roy the Robber has seen a lot of American movies, and kno ...
- HDOJ 2955 Robberies (01背包)
10397780 2014-03-26 00:13:51 Accepted 2955 46MS 480K 676 B C++ 泽泽 http://acm.hdu.edu.cn/showproblem. ...
- HDU 2955 【01背包/小数/概率DP】
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDOJ.2955 Robberies (01背包+概率问题)
Robberies 算法学习-–动态规划初探 题意分析 有一个小偷去抢劫银行,给出来银行的个数n,和一个概率p为能够逃跑的临界概率,接下来有n行分别是这个银行所有拥有的钱数mi和抢劫后被抓的概率pi, ...
随机推荐
- 新技术---- MongoDB
MongoDB 在 CentOS7 上安装 MongoDB-------https://www.linuxidc.com/Linux/2016-06/132675.htm Centos7 安装mong ...
- hdu2046
递推题目,由于只有一种瓷砖,1*2规格的,所以一块2*2的面积上只能有2种摆放方式,然后就跟上台阶那个差不多,上一步或者上两部. 就是f[n]=f[n-1]+f[n-2]; 另外注意__int64的问 ...
- WebLoad XML-parser methods
WebLOAD provides an embedded, third-party XML parser object to improve the multi-platform support fo ...
- 关于ul中li不对齐的问题
将li中加入 overflow:hidden; 即可. 同时overflow:auto 可以控制滚动条的出现.
- 计算几何 I. 极角
参考资料 hankcs.com: POJ 1981 Circle and Points 题解 aswmtjdsj: POJ 1981 Circle and Points [定长圆覆盖最多点问题] zx ...
- BZOJ2246 [SDOI2011]迷宫探险 【记忆化搜索dp + 概率】
题目 输入格式 输出格式 仅包含一个数字,表示在执行最优策略时,人物活着走出迷宫的概率.四舍五入保留3位小数. 输入样例 4 3 3 2 .$. A#B A#C @@@ 143 37 335 85 9 ...
- BZOJ1923 [Sdoi2010]外星千足虫 【高斯消元】
题目 输入格式 第一行是两个正整数 N, M. 接下来 M行,按顺序给出 Charles 这M次使用"点足机"的统计结果.每行 包含一个"01"串和一个数字,用 ...
- P1282 多米诺骨牌 (差值DP+背包)
题目描述 多米诺骨牌有上下2个方块组成,每个方块中有1~6个点.现有排成行的 上方块中点数之和记为S1,下方块中点数之和记为S2,它们的差为|S1-S2|.例如在图8-1中,S1=6+1+1+1=9, ...
- android本地存储SharedPreferences
SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的就是一个key-value(键值对)SharedPreferences常用来 ...
- html5对各浏览器的支持情况
考虑到HTML5标准的制定原则:新特性基于HTML.CSS.DOM 以及 JavaScript:减少对外部插件的需求(比如Flash):独立于设备等,我们选取了HTML 5的几项主要特性来评价浏览器系 ...