HDU 2191 robberies Robbery (AC code) 01 backpack variants

Source: Internet
Author: User

1#include <iostream>2 #defineLimit 1103 using namespacestd;4 intN;5 intMoney[limit];//the bank's money.6 DoubleSafe[limit];//the probability of being caught7 Doubledp[10000];8 DoubleP,big;9 voidCalintTempintN//all the bank's money, N home BankTen { One     inti,j; A      for(i=0; i<n;i++) -          for(j=temp;j>=money[i];j--)//J is the current bank's money -             if(dp[j]< (Big=dp[j-money[i]]*safe[i]))//probability is multiplication!  theDp[j]=big;//multiplication takes too long to calculate, so use the assignment Method! Can write like this dp[j]=dp[j-money[i]]*safe[i] - } - voidMain ()//How many probabilities would it take for J to rob so much money ? - { +     intt,i,temp; -scanf"%d",&t); +      while(t--) A     { atscanf"%lf%d", &p,&n);//caught upper limit p, n banks -temp=0; -p=1-p; -Memset (DP,0,sizeof(DP)); -dp[0]=1.0;//without the money, the security probability is 1. -          for(i=0; i<n;i++) in         { -scanf"%d%lf", &money[i],&safe[i]);//The amount of money, the probability of being caught tosafe[i]=1-safe[i];//Turn safety probability +temp+=Money[i]; -         } the cal (Temp,n); *          for(I=temp; i>=0; i--) $             if(dp[i]-p>0.0) {printf ("%d\n", i); Break;}//dp[i]-p>0.0 said that the security probability is greater than the requirement,Panax Notoginseng /* - dp[] The first part may not be ordered in reverse order, that is, not from large to small arrangement.  the For example: When all the money in the bank is more than 2, then dp[1] is 0, + according to the 13th line of J>=money[i], no one will be updated to dp[1], then dp[0]=1,dp[1]=0 .... The back dp[>2] will be greater than 0.  A but starting from behind is ascending, and only part of it.  the */ +     } -}
2955

Test instructions: To Rob, but Rob every bank has the probability of being caught, ask how much money can be robbed under the condition of the probability of being caught below the stipulated.

Input: The first behavior T, which represents a total of t test examples. The first line of each example gives a floating-point P, which is the upper limit of the probability of being caught. The first line also has an integer n, which is ready to rob the N banks. Next there are n rows representing n banks, each line being an integer m and a floating point p. M represents the amount of money in this bank, and P robs the bank of the probability of being caught.

Output: Below the prescribed probability of being caught, how much money can rob?

idea : Notice that this is the probability! Do you see this question when you want to take the bank as the 1th for loop, take the probability size as the 2nd for loop, the DP array save probability size? So is the 2nd for loop going to define J as a floating point or an integral type? The probability is not greater than 1. This road is not working, you want to try to enlarge this probability 100 times times? Are you sure one of the banks is likely to be 0.000000005? 100 times times enough? So the probability of amplification is very large, this is not a big cost? is efficiency permissible?

The main idea : The probability of being caught into a safe probability, safety probability =1-The probability of being caught, DP is the security probability of saving. The bank as the 1th for loop, that is, the first I bank can rob the case, how to rob more without being caught. Take the amount of money as the 2nd for loop, the upper limit is the unwilling of each bank and the lower limit is the money in the I bank. Every time an update dp[j] represents the maximum security probability of being able to grab J money. That said, the capacity of the backpack is the unwilling of all banks and the probability that value is safe. How much should the DP array fit? Fortunately hdu leave a path, open dp[10000] is enough. How do I finally get the answer? The answer is not in dp[], dp[] is the safe probability of grabbing J money. Ideally, this array is ordered in reverse order, and the more you rob, the less secure the probability, the more dangerous it is. In fact, we have to consider the worst case scenario. The analysis is as follows:

(1) Assuming that the money in each bank is at least y, when y>1, the value of dp[1] to Dp[y] is 0 before and after the calculation, and these elements will not be updated to find the answer in line 13th.

(2) If banks have more money, their security is more likely. It's not common sense, but it has to be a bit of a defense.  For example, the 1th bank money is 1, the security probability 0.7, the 2nd bank money is 2, the security probability is 0.9, then dp[0]=1,dp[1]=0.7,dp[2]=0.9,dp[3]=0.7*0.9. DP value from 1→0.7→0.9→0.63, that is, from big → small → big → small. How to find the safe probability of the regulation?

(3) Suppose that the money for a bank is the largest of all banks, X. The dp[] array, where the subscript is greater than x in each loop of the 1th for is likely to be updated once, then the value of this dp[] will not appear 0. But will it be ordered in reverse order? It should be! I haven't proved it yet! 囧 The ideal will reverse order, but success.

HDU 2191 robberies Robbery (AC code) 01 backpack variants

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.