概率dp hdu2599

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

這道題我一眼看,這不就是個01揹包的板子嗎,後來發現概率不能相加。。。。。。

我看了好久的題目:發現這被抓的概率越乘越小是什麼鬼,偷得越多還越難抓了?但是反過來想就很容易理解了,

第一個樣例給出了要小於0.04的風險,那麼我們逃跑的概率就最少0.96,我們在下面的計算中只需要保證我們逃跑概率大於0.96就行了.

但是這裏有個問題,我們在寫01揹包的時候,dp[j]這裏面的j表示的當前揹包的容量,dp[j]表示的當前的最大價值,要是換成這道題我們的慣性思維就是把概率當成揹包容量,由於數組的下標不能爲浮點,所以我們只能反過來.    dp[j]表示在價值爲j時逃跑概率的最大值就完美的解決了.

狀態轉移:

for(int i = 1; i <= n; i++){//從第一個銀行開始遍歷
        for(int j = sum; j >= val[i]; j--){//假設已經偷了第i個銀行
            double k = (1 - w[i] * 1.0) * dp[j - val[i]];//偷了第i個銀行,剩餘價值j - val[i]
            dp[j] = max(dp[j],k);
        }
      }

下面是ac代碼

#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <ctype.h>
#include <bitset>
#define  LL long long
#define  ULL unsigned long long
#define mod 10007
#define INF 0x7ffffff
#define mem(a,b) memset(a,b,sizeof(a))
#define MODD(a,b) (((a%b)+b)%b)
using namespace std;
const int maxn = 100010;
int n,m,cnt;
int val[maxn];
double w[maxn];
double dp[maxn];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
      double V;
      int n;
      int sum = 0;
      scanf("%lf%d",&V,&n);
      for(int i = 1; i <= n; i++){
        scanf("%d%lf",&val[i],&w[i]);
        sum += val[i];
      }
      V = 1 - V;
      mem(dp,0);
      dp[0] = 1;
      for(int i = 1; i <= n; i++){
        for(int j = sum; j >= val[i]; j--){
            double k = (1 - w[i] * 1.0) * dp[j - val[i]];
            dp[j] = max(dp[j],k);
        }
      }
      int ans = 0;
      //printf("%f\n",dp[sum]);
      for(int i = sum; i >= 0; i--){//找一個最大的i,滿足要求就行了
        if(dp[i] > V){
            ans = i;
            break;
        }
      }
      printf("%d\n",ans);
    }




    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章