HDU 2955 robberies (0 1 backpack)

Source: Internet
Author: User
Robberies

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 8096 accepted submission (s): 3063

Problem descriptionthe 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 varous 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.

Inputthe first line of input gives t, the number of instances. 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. 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.

Outputfor each test case, output a line with the maximum number of millions he can perform CT to get while the probability of getting caught is less than the limit set.

Notes and constraints
Zero <t <= 100
0.0 <= P <= 1.0
0 <n <= 100
0 <MJ <= 100
0.0 & lt; = PJ & lt; = 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

30.04 31 0.022 0.033 0.050.06 32 0.032 0.033 0.050.10 31 0.032 0.023 0.05

Sample output

246

Sourceidi open2009

RecommendgaojieQuestion: Give n Banks the corresponding amount of money in each bank [I]. Money and the probability PI that the bank is caught. Ask the maximum amount of money that can be obtained without being caught (the probability of being caught is smaller than the given P value.Analysis1. Rob n Banks. If they are not caught, they are not caught each time. The multiplication principle is used to process the probability that they are not caught each time. 2. convert to a 0-1 backpack: Use the total amount of money in all banks as the volume V of the backpack, and use the probability of not being caught as the cost [I], cost [I] is added to a general 0-1 backpack. Here, because it is a multiplication principle, it should be converted to multiplication. In this way, the accumulation problem of the traditional 01 backpack becomes the base multiplication. 3. state transition equation:DP [J] = max (DP [J], DP [J-bank [I]. Money] * Bank [I]. Run)DP [J] indicates the maximum probability of not being caught when the J account is robbed.
Code:

# Include <cstdio> # include <iostream> # include <cmath> # include <cstring> using namespace STD; struct node {int money; double run; // probability of not being captured} bank [105]; double DP [105*105]; // if the size of the array is set to 105, the reint main () {double T, P; int T, I, n, J, maxmoney; scanf ("% d", & T); While (t --) {maxmoney = 0; scanf ("% lf % d", & P, & N); P = 1.0-p; // The maximum probability of being caught is converted to the minimum probability of not being caught for (I = 1; I <= N; I ++) {scanf ("% d % lf", & Bank [I]. money, & T); bank [I]. run = 1.0-t; // the probability of being captured. Maxmoney + = Bank [I]. money;} memset (DP, 0, sizeof (DP); // Initialization is 0 not-1 DP [0] = 1.0; // The probability of not snatching is 0 for (I = 1; I <= N; I ++) // the money of the I bank {for (j = maxmoney; j> = Bank [I]. money; j --) DP [J] = max (DP [J-bank [I]. money] * Bank [I]. run, DP [J]) ;}for (I = maxmoney; I >=0; I --) // starts enumeration from the maximum number of money {If (DP [I]> P) {printf ("% d \ n", I); break ;}}return 0 ;}/ * for (I = 1; I <= N; I ++) {for (j = maxmoney; j> = Bank [I]. money; j --) DP [J] = max (DP [J-bank [I]. money] * Bank [I]. run, DP [J]);} DP [J-bank [I]. Money] indicates the probability that the bank that has been robbed will not be arrested except the bank. Similar to the combination number. In combination with the first example: in this way, the DP [0] (known) is calculated in sequence) DP [1] DP [3] DP [2] DP [6] DP [5] DP [4] */

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.