HDU2955-Robberies

简介:
Robberies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10562    Accepted Submission(s): 3889

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
 

动态规划
AC代码:

//用成功逃走的概率当做价值,银行的总钱数当做背包容量 

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
   int M;//钱量 
   float P;//不被抓概率 
}dp[110];
float f[10010];//最大的钱量为100*100=10000; 
int max(int n,int m)
{return n>m?n:m;}
int main()
{
    int i,j,T,n,m,sum;
    float x,Psum;
    scanf("%d",&T);
    while(T--)
    {
       memset(dp,0,sizeof(dp));
       memset(f,0,sizeof(f));
       scanf("%f %d",&x,&m);
       Psum=1-x;//最小逃脱率,低于这个就逃不了了 
       sum=0;
       for(i=0;i<m;i++)
       {
          scanf("%d %f",&dp[i].M,&dp[i].P);
          sum+=dp[i].M;
          dp[i].P=1-dp[i].P;//逃脱率 
       }
       
       f[0]=1;//抢0块大洋肯定不被抓
       
       for(i=0;i<m;i++)
       {
          for(j=sum;j>=dp[i].M;j--)
          f[j]=max(f[j],f[j-dp[i].M]*dp[i].P);//f[j]表示抢j块大洋的最大的逃脱概率,条件是f[j-q[i].money]可达,也就是之前抢劫过;
       }
       for(i=sum;i>=0;i--)
       {
          if(f[i]>=Psum)
          {break;}
       }
       printf("%d\n",i);
    }
    return 0;
}

相关文章
|
算法 Java
HDU 2084 数塔
在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?
174 0
|
Java 人工智能
|
存储
hdu 2203 亲和串
点击打开链接hdu 2203 思路:kmp 分析: 1 题目要求的是给定字符串s1 和 s2,问s1能否通过移位得到使得s2包含在s1里面。
818 0
hdu 1305 Immediate Decodability
点击打开链接hdu1305 思路:字典树 分析: 1 题目要求的是是否有一个字符串作为其它字符串的前缀 2 利用字典树的性质在插入的时候就可以判断某一个字符串是否是其它字符串或当前字符串是其它字符串的前缀 3 多组数据利用静态分配不能用动态分配。
749 0