Robberies 杭电

Poor POIUYTREWQ recently wanted to buy merchandise at dota2, but short of money. He remembered a large've seen before, that bank robbery might be a good choice. He believes that the bad guys caught because there is no pre-planning. So he conducted an assessment of major banks in the preceding months; assessment including security and theft Amount: He wants to know the maximum amount at a certain risk factor can steal 

Input The first line gives an integer T, T expressed test data set for each set of data, the first line gives a floating point P, represents the probability of the maximum allowed POIUYTREWQ caught, and an integer N, the he plans to rob a bank of N Next N lines, each given a number Mj integer and floating-point Pj. 
bank robbery j Mj millions of dollars available, the probability of being caught is Pj .Output for each test, each output line an integer representing the most money POIUYTREWQ caught in a case where the probability is less than P, can grab. 

And the Constraints the Notes 
0 <T <100 = 
0.0 <= P <= 1.0 
0 <N <= 100 
0 <Mj of <= 100 
0.0 <= Pj of <= 1.0 
You can think of each bank are independent. 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 
Sample this topic is really pit ah ,,, when I first started to escape probability as the total capacity of the pack, as the price value, and my heart still thinking about it does not matter floating point ,, it is multiplied by 1e3 , and then as a very simple problem to do 01 backpack, so the sample may actually passed. . . .
The subject of the correct approach is to put money in the bank as the total capacity of the backpack, and then escape probability as a value, that is the total number of backpack capacity of the bank when the money is able to escape probability ,, with an array of deposit at dp then the total capacity of the entire backpack is traversed to find the next biggest escape the probability
of. . Corresponding capacity is the final answer
// entitled to the money and each bank's probability of being caught, due to grab as much money, so make sure to try not to be caught,
 // but after a number of banks do not grab the probability of being caught is robbed every bank product of the probability of being caught, I actually put this forgot!
// led me to take a lot of detours, ideas can not be too dead! DP [] represents a time index corresponding to grab the money, the probability of this case is not caught,
 // title given above can not be caught final probability P, the probability of being caught can not be less than (1-P ),
 // so in the end only needs to reverse traverse DP, first find a greater than or equal to dp 1-P [i], it is possible to ensure the maximum i, that is to grab the most money. 
#include <the iostream> 
#include <cstdio> 
#include <CString>
 the using  namespace STD;
 const  int N = 1e6;
 Double the commit [N];
 int . price [N];
 Double DP [N]; 

int main () {
     int T ; 
    cin >> t;
    while(t--){
        double s;
        int sum=0;
        int n;
        cin>>s>>n;
        for(int i=1;i<=n;i++){
            cin>>price[i]>>commit[i];
            sum+=price[i];
        }
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        for(int i=1;i<=n;i++){
            for(int j=sum;j>=price[i];j--){//将价格看成背包的容量 
                dp[j]=max(dp[j] , dp[j-price[i]] * (1-commit[i]));
            }
        }
        
        for(int i=sum;i>=0;i--){
            if(dp[i]>=(1-s)){
                cout<<i<<endl;
                break;
            }
        }
    }
    return 0;
}

 






Guess you like

Origin www.cnblogs.com/Accepting/p/11279108.html