Just another robbery (backpack)

Source: Internet
Author: User

1079-just another robbery
PDF (中文版) Statistics Forum
Time Limit:4 second (s) Memory limit:32 MB

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.

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 P J . Bank J contains Mjmillions, 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.0494which is greater than the given probability (0.04). That's why he had only option, just to rob Rank 2.

The main puzzle: give you a total probability p, and then give you n a bank, inside the rich VI, the probability of being caught is pi, to be caught probability is less than p, that is, the probability of not being caught is greater than 1-p, the total amount of money as the capacity of the backpack, the probability as the value of the backpack;

Code:

#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include < algorithm> #include <vector> #include <queue> #include <stack> #include <map>using namespace std;const int Inf=0x3f3f3f3f;const Double Pi=acos ( -1.0); typedef long long LL; #define MEM (x, y) memset (x,y,sizeof) # Define PI (x) printf ("%d", x) #define PL (x) printf ("%lld", x) #define SI (x) scanf ("%d", &x) #define SL (x) scanf ("%lld",  &AMP;X) #define P_ printf ("") #define T_T while (t--) const int maxn=110;struct node{double P;int w;friend bool Operator < (Node A,node B) {if (A.P&LT;B.P) return true;else return false;}}; Double bag[10010]; Node Dt[maxn];int Main () {int t,kase=0;si (T); T_t{double p;int n;scanf ("%lf%d", &p,&n); int sum=0;for (int i=0;i<n;i++) scanf ("%d%lf",&dt[i].w,& DT[I].P), Sum+=dt[i].w;sort (Dt,dt+n), mem (bag,0); bag[0]=1;for (int i=0;i<n;i++) {for (int j=sum;j>=dt[i].w;j--) Bag[j]=max (bag[j],bag[j-dt[i].w]* (1-DT[I].P));} for (int i=sum;i>=0;i--) {if (bag[I]>1-p) {printf ("Case%d:%d\n", ++kase,i); return 0;}

  

Just another robbery (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.