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算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少?
152 0
|
算法 Java 人工智能
|
Java BI
HDU 1412 {A} + {B}
{A} + {B} Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19833    Accepted Submission(s): 8245 Problem Description 给你两个集合,要求{A} + {B}.
818 0
hdu 1856 More is better
点击hdu 1856思路: 思路: 离散化+并查集 分析: 1 点数最多为10^7,但是边数最多10^5,所以我们必须采用离散化,然后利用带权并查集的思想,rank[x]表示的是以x为根节点的集合的元素个数 2 这一题主要注意的就是当...
814 0
|
存储
hdu 2609 How many
点击打开链接hdu 2609 思路:字符串的最小表示 分析: 1 题目要求的是给定n个字符串,找出不同的字符串的个数。由于题目说了,字符串可以进行变换,也就是如果两个字符串相同那么它们的最小表示是相同的。
740 0