1. 程式人生 > >HDU 2955

HDU 2955

amount decided targe work pen AD usually floating 很難

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2955

Robberies

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


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 分析: 英文題目是真的很難理解啊。。。。。 先是給出幾組數據,每組數據第一行是總被抓概率p(最後求得的總概率必須小於他,否則被抓),然後是想搶的銀行數n。然後n行,每行分別是該銀行能搶的錢數m[i]和被抓的概率p[i],在成功逃跑的前提下獲得的最大錢數 開始是想將被抓的概率當作背包總容量,每個銀行的錢當作物品價值,被抓概率當作物品重量,這樣思路確實是對的,但是這題的數據很毒,被每個銀行被抓的概率精度是不確定的,不一定是小數點後兩位,可能是0.00001,所以概率乘以100是沒有用的,乘太大也就會超出數組的下標界限 所以轉變思維: 將銀行錢總數當作背包的容量,每個銀行逃跑的概率做價值,每個銀行的錢數當作重量 這樣問題就轉變成了怎麽拿可以使得逃跑概率最大 dp[k]的意義:拿k錢的時候的成功逃跑概率 從後往前遍歷dp數組,找到第一個dp[k]大於題目給出的逃跑概率限制條件,這個時候的k值就是可以獲得的最大錢數 註意: 1.題目給的限制概率是被抓的概率(p),逃跑的概率=1-被抓的概率 小偷偷完之後被抓的概率要小於p,小偷偷完之後逃跑的概率要大於(1-p) 2.每個銀行題目給的概率是偷每個銀行被抓的概率,偷每個銀行成功逃跑的概率=(1-偷每個銀行被抓的概率) 3.成功逃跑的概率等於偷每個銀行成功逃跑概率的乘積 emmm,應該註意點就這些 放代碼:
#include<bits/stdc++.h>
using namespace std;
#define max_v 10005
double dp[max_v];//拿k錢的時候的成功逃跑概率
int w[max_v];//每個銀行的錢數當作重量
double v[max_v];//每個銀行逃跑的概率做價值
//所有銀行的總錢數做背包容量
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        double p;
        int n;
        int sum=0;
        scanf("%lf %d",&p,&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d %lf",&w[i],&v[i]);
            sum=sum+w[i];
        }
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        for(int i=0;i<n;i++)
        {
            for(int j=sum;j>=w[i];j--)
            {
                dp[j]=max(dp[j],dp[j-w[i]]*(1-v[i]));
            }
        }
       for(int i=sum;i>=0;i--)
       {
           if(dp[i]>(1-p))
           {
               printf("%d\n",i);
               break;
           }
       }
    }
    return 0;
}

HDU 2955