动态规划:HDU-2955-0-1背包问题:Robberies
解题心得:
- 这题涉及概率问题,所以要运用概率的知识进行解答。题目要求不被抓到的概率,但是给出的是被抓到的概率,所要用1减去后得到答案。最好使用double类型,避免精度问题导致WA。
- 先算出可以抢劫的总钱数,以此动态规划。
Robberies
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 23012 Accepted Submission(s): 8489
Problem Description
The 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.
Input
The 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 .
Output
For 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.
Sample Input
3
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
Sample Output
2
4
6
Source
IDI Open 2009
#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
const int maxsize = 10010;
struct ai
{
double p;
int v;
}a[maxsize];
int main()
{
long long totle;
double d[maxsize];
int t;
cin>>t;
while(t--)
{
totle = 0;
int n;
double pro;
cin>>pro>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].v>>a[i].p;
totle += a[i].v;
}
d[0] = 1;//这个初始化很重要。
for(int i=1;i<=totle;i++)
d[i] = 0;
for(int i=1;i<=n;i++)
{
for(int j=totle;j>=a[i].v;j--)
{
d[j] = max(d[j],d[j-a[i].v]*(1-a[i].p));
}
}
for(int i=totle;i>=0;i--)
{
if(d[i] >= (1-pro))
{
cout<<i<<endl;
break;
}
}
}
return 0;
}
动态规划:HDU-2955-0-1背包问题:Robberies的更多相关文章
- HDU 2955(01背包问题)
M - 01背包 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descript ...
- HDU 2955(0-1背包问题)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/M 题目: Description The aspir ...
- HDU 2955 Robberies 背包概率DP
A - Robberies Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
- 详解动态规划(Dynamic Programming)& 背包问题
详解动态规划(Dynamic Programming)& 背包问题 引入 有序号为1~n这n项工作,每项工作在Si时间开始,在Ti时间结束.对于每项工作都可以选择参加与否.如果选择了参与,那么 ...
- 经典递归问题:0,1背包问题 kmp 用遗传算法来解背包问题,hash表,位图法搜索,最长公共子序列
0,1背包问题:我写笔记风格就是想到哪里写哪里,有很多是旧的也没删除,代码内部可能有很多重复的东西,但是保证能运行出最后效果 '''学点高大上的遗传算法''' '''首先是Np问题的定义: npc:多 ...
- HDU 4370 0 or 1 (最短路+最小环)
0 or 1 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/R Description Given a n*n matrix ...
- 蓝桥杯 0/1背包问题 (java)
今天第一次接触了0/1背包问题,总结一下,方便以后修改.不对的地方还请大家不啬赐教! 上一个蓝桥杯的例题: 数据规模和约定 代码: import java.util.Scanner; public ...
- HDU - 4370 0 or 1
0 or 1 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- Java实现动态规划法求解0/1背包问题
摘要: 使用动态规划法求解0/1背包问题. 难度: 初级 0/1背包问题的动态规划法求解,前人之述备矣,这里所做的工作,不过是自己根据理解实现了一遍,主要目的还是锻炼思维和编程能力,同时,也是为了增进 ...
- Hdu 2955 Robberies 0/1背包
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
随机推荐
- SQLServer常见性能问题
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- CI框架更新与删除
$this->load->database(); // $query=$this->db->get('t_repayments'); // $res ...
- C#---vs2010发布、打包安装程序程序(转载)
转载地址:点击打开 1. 在vs2010 选择“新建项目”→“ 其他项目类型”→“ Visual Studio Installer→“安装项目”: 命名为:Setup1 . 这是在VS2010中将有三 ...
- springboot 学习笔记(九)
springboot整合activemq,实现broker集群部署(cluster) 1.为实现jms高并发操作,需要对activemq进行集群部署,broker cluster就是activemq自 ...
- ECLIPSE 取消自动更新
经常遇到一开eclipse 时,一直很卡的问题,发现是它一直尝试联网更新东西 ,如maven 所以解决办法 , eclipse 取消自动更新的方法: 1. window --> prefere ...
- linux 命令——25 linux文件属性详解
Linux 文件或目录的属性主要包括:文件或目录的节点.种类.权限模式.链接数量.所归属的用户和用户组.最近访问或修改的时间等内容.具体情况如下: 命令: ls -lih 输出: [root@loca ...
- 如何解析比特币中的交易原始数据rawData
交易数据结构 有关交易的详细信息可以查看比特币的wiki网站:Transaction TxBinaryMap: 原始图片地址 交易的结构表格(Transaction): 示例数据 以一个正式网络的一笔 ...
- iOS开发:自定义带下划线文本的UIButton
Uiunderlinedbutton.h代码 @interface UIUnderlinedButton : UIButton { } + (UIUnderlinedButton *) underli ...
- 【洛谷1501】[国家集训队] Tree II(LCT维护懒惰标记)
点此看题面 大致题意: 有一棵初始边权全为\(1\)的树,四种操作:将两点间路径边权都加上一个数,删一条边.加一条新边,将两点间路径边权都加上一个数,询问两点间路径权值和. 序列版 这道题有一个序列版 ...
- 【洛谷4149】[IOI2011] Race(点分治)
点此看题面 大致题意: 给你一棵树,问长度为\(K\)的路径至少由几条边构成. 点分治 这题应该比较显然是点分治. 主要思路 与常见的点分治套路一样,由于\(K≤1000000\),因此我们可以考虑开 ...