hdu 2955 Robberies(01背包)
来源:互联网 发布:淘宝看价格走势软件 编辑:程序博客网 时间:2024/04/29 19:00
http://acm.hdu.edu.cn/showproblem.php?pid=2955
Robberies
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.
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 .
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.
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
30.04 31 0.022 0.033 0.050.06 32 0.032 0.033 0.050.10 31 0.032 0.023 0.05
Sample Output
246
分析:01背包的概率问题
当前的概率基于前一种状态的概率,即偷n家银行而不被抓的概率等于偷n-1家银行不被转的概率乘以偷第n家银行不被抓的概率。
用dp[i]表示偷价值为 i 时不被抓的概率,则状态转移方程为:
dp[j] = max(dp[j] , dp[j-m[i]] * (1-p[i]));
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>#include <vector>#include <map>using namespace std;#define N 55000#define INF 0x3f3f3f3f#define PI acos (-1.0)#define EPS 1e-8#define met(a, b) memset (a, b, sizeof (a))typedef long long LL;double dp[N];int main (){ int t; scanf ("%d", &t); while (t--) { double P, p[N]; int n, m[N]; met (dp, 0); dp[0] = 1; scanf ("%lf %d", &P, &n); P = 1-P; int sum = 0; for (int i=1; i<=n; i++) { scanf ("%d %lf", &m[i], &p[i]); sum += m[i]; p[i] = 1-p[i]; } for (int i=1; i<=n; i++) for (int j=sum; j>=m[i]; j--) dp[j] = max (dp[j-m[i]]*p[i], dp[j]); for (int i=sum; i>=0; i--) if (dp[i] >= P) { printf ("%d\n", i); break; } } return 0;}
0 0
- hdu 2955 Robberies(01背包)
- hdu 2955 Robberies(01背包)
- hdu 2955 Robberies(01背包)
- hdu 2955 Robberies(01背包)
- hdu 2955 Robberies(01背包)
- HDU 2955 Robberies (01背包)
- hdu-2955(01背包)Robberies
- HDU 2955 Robberies(01背包变型)
- HDU 2955 Robberies (01背包)
- HDU 2955 Robberies(01背包)
- HDU 2955 Robberies (01背包)
- HDU - 2955 Robberies(01背包)
- HDU 2955 Robberies(01背包问题)
- hdu 2955 Robberies(01背包)
- hdu 2955 Robberies(01背包)
- HDU 2955 Robberies(01背包变形)
- HDU 2955 Robberies(01背包)
- HDU 2955-Robberies(01背包变形)
- 如何让Keil MDK兼容Keil C51?
- android 键盘弹出时将屏幕遮挡
- enum 学习笔记
- C#程序中获取各种目录
- 关于listview设置item高度没有效果问题
- hdu 2955 Robberies(01背包)
- android bundle存放数据详解
- 开篇
- 简单的谈下自己的一些概括,我就是这样的一个人啦。呵呵
- 界面美化攻略
- 其实,我对一个php站点,比较有印象的,详细分享下
- DataSet和List相互转换
- C++输入输出简介
- Android应用性能优化实践