LightOJ1079---Just another robbery (probability do 01 backpack)

Source: Internet
Author: User

As Harry Potter series is over, and Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But he friends-hermione and Ron have decided upon a tolerable probability P of getting caught. They feel that he's safe enough if the banks he robs together give a probability less than P.
Input

Input starts with an integer T (≤100), denoting the number of test cases.

Each case contains a real number P, the probability Harry needs to being below, and an integer N (0 < n≤100), the number Of banks he has plans for. Then follow N lines, where line J gives an integer Mj (0 < mj≤100) and a real number Pj. Bank J contains Mj millions, and the probability of getting caught from robbing it's Pj. A Bank goes bankrupt if it is robbed, and your may assume that all probabilities be independent as the police have very lo W funds.
Output

For each case, print the case number and the maximum number of millions he can expect to get while the probability of Gett ING caught is less than P.
Sample Input

Output for 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

Case 1:2

Case 2:4

Case 3:6
Note

For the first case, if he wants to rob Bank 1 and 2, then the probability of getting caught is 0.02 + (1-0.02) *. 03 = 0 .0494 which is greater than the given probability (0.04). That's why he had only option, just to rob Rank 2.

probability do 01 backpack
Dp[i] indicates the maximum probability of not being caught when I am robbed

/************************************************************************* > File name:d.cpp > Author:al ex > Mail: [email protected] > Created time:2015 April 29 Wednesday 20:07 38 seconds ************************************** **********************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;Static Const intN =10010;DoubleDp[n];DoubleP[n];intW[n];intMain () {intTintIcase =1;scanf("%d", &t); while(t--) {intsum =0;DoublePintNscanf("%lf%d", &p, &n); for(inti =1; I <= N; ++i) {scanf("%D%LF", &w[i], &p[i]); P[i] =1.0-P[i];        Sum + = W[i]; } for(inti =0; I <= sum; ++i) {Dp[i] =0; } dp[0] =1.0; for(inti =1; I <= N; ++i) { for(intj = sum; J >= W[i]; --J) {if(Dp[j-w[i]])                {Dp[j] = max (Dp[j], dp[j-w[i]] * p[i]); }            }        }printf("Case%d:", icase++); for(inti = sum; I >=0; -I.) {if(P >=1-Dp[i]) {printf("%d\n", i); Break; }        }    }return 0;}

LightOJ1079---Just another robbery (probability do 01 backpack)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.