CQUOJ 2842 - Robberies
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 .
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
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
/* 2016年4月19日00:45:44 题目大意: 一小偷要抢多个银行 求在不被抓的前提下 所能抢的最多的大洋 题目要看清 第一个 p 为题目要求的 要低于等于 的被抓的概率 后面的为 抢劫各银行被抓的概率 状态转移方程 dp[j] = max(dp[j], dp[j-bank[i].m] * (1-bank[i].v)) 其中 dp[j] 为抢 j 个大洋所能逃走的最大概率 初始化 dp[0] = 1, 其他都为 0 */
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <queue> 6 # include <vector> 7 # define INF 0x3f3f3f3f 8 using namespace std; 9 10 struct Node 11 { 12 int m; 13 double p; 14 }bank[105]; 15 16 int main(void) 17 { 18 double dp[10005]; 19 int n, i, j, test, sum; 20 double p0; 21 scanf("%d", &test); 22 while (test--){ 23 scanf("%lf %d", &p0, &n); 24 sum = 0; 25 for (i = 1; i <= n; i++){ 26 scanf("%d %lf", &bank[i].m, &bank[i].p); 27 sum += bank[i].m; 28 } 29 memset(dp, 0, sizeof(dp)); 30 dp[0] = 1; 31 for (i = 1; i <= n; i++){ 32 for (j = sum; j >= bank[i].m; j--){ 33 dp[j] = max(dp[j], dp[j-bank[i].m]*(1-bank[i].p)); 34 } 35 } 36 for (i = sum; i >= 0; i--){ 37 if (dp[i] >= 1-p0){ 38 printf("%d\n", i); 39 break; 40 } 41 } 42 } 43 44 return 0; 45 }
下面是java版本的
1 import java.util.*; 2 import java.io.*; 3 4 public class Main { 5 6 public static void main(String[] args) { 7 //System.out.println("aaaa"); 8 Scanner cin = new Scanner(System.in); 9 double[] dp = new double[10005]; 10 double[] bank_p = new double[102]; 11 int[] bank_m = new int[102]; 12 int test, i, j, sum, n; 13 double p; 14 test = cin.nextInt(); 15 while (test!=0){ 16 test--; 17 sum = 0; 18 p = cin.nextDouble(); 19 n = cin.nextInt(); 20 //memset(dp, 0, sizeof(dp)); 21 for (i = 1; i < 10005; i++) 22 dp[i] = 0; 23 dp[0] = 1; 24 for (i = 1; i <= n; i++){ 25 bank_m[i] = cin.nextInt(); 26 bank_p[i] = cin.nextDouble(); 27 sum += bank_m[i]; 28 } 29 for (i = 1; i <= n; i++){ 30 for (j = sum; j >= bank_m[i]; j--){ 31 dp[j] = max(dp[j], dp[j-bank_m[i]]* (1-bank_p[i])); 32 } 33 } 34 for (i = sum; i >= 0; i--){ 35 if (dp[i] >= (1-p)){ 36 System.out.println(i); 37 break; 38 } 39 } 40 } 41 cin.close(); 42 } 43 static double max(double a, double b){ 44 if (a > b) return a; 45 return b; 46 } 47 48 }

浙公网安备 33010602011771号