Topic Connection: http://acm.hdu.edu.cn/showproblem.php?pid=1203
I need A offer!
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 22401 Accepted Submission (s): 8959
Problem descriptionspeakless very early want to go abroad, now he has finished all the required examinations, prepared all the materials to prepare, so, they need to apply for school. To apply for any university abroad, you have to pay a certain amount of application fees, which is very alarming. Speakless didn't have much money, only a total of n million dollars. He will choose a number of M schools (certainly within his financial range). Each school has a different application fee of a (million dollars), and speakless estimates the likelihood of his getting a offer from this school B. Whether or not there is an offer between different schools will not affect each other. "I need a offer," he shouted. Help the poor man, help him calculate the maximum probability that he can receive at least one offer. (if Speakless chooses more than one school, get an offer from any school).
Input has several sets of data, and the first row of each group of data has two positive integers n,m (0<=n<=10000,0<=m<=10000)
In the following M-line, each row has two data ai (integer), and Bi (real) represents the application fee for the I-school and the probability of getting an offer.
The last entry has two 0.
Output each set of data corresponds to an export, indicating the maximum probability that speakless may get at least one offer. Expressed as a percentage, accurate to one decimal place.
Sample Input10 34 0.14 0.25 0.30 0
Sample output44.0%
HintYou should use printf ("percent") to print a '% '. Solution: There are two ways of thinking 1.dp[i][j] to represent the biggest possibility that the volume of j is employed, the transfer equation should be used to the knowledge of a little probability theory, The transfer equation is still using the article I can be loaded or not to write, dp[i][j] = max (dp[i-1][j],1-(1-dp[i-1][j-a[i]) * (1-d[i])); 2.DP[I][J] Represents the first I-item, which occupies the smallest probability that the volume of j is not employed, the transfer equation dp[i][j] = max (dp[i-1][j],dp[i-1][j-a[i]]* (1-d[i)), and note that both of these methods compress storage space, Compression to 1-D is not super-memory gives two kinds of code: the first method
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 Const intN =10005;6 intA[n];7 DoubleB[n];8 DoubleDp[n];9 DoubleMaxDoubleADoubleb)Ten { One if(A<B)returnb; A Else returnA; - } - intMain () the { - intn,m; - while(~SCANF ("%d%d",&n,&m)) - { + if(n==0&&m==0)return 0; -Memset (DP,0,sizeof(DP)); + for(inti =1; I <= m; i++) A { atscanf"%D%LF",&a[i],&b[i]); - } - for(inti =1; I <= m; i++) - { - for(intj = N; J >= A[i]; j--) - { inDP[J] = max (Dp[j],1-(1-dp[j-a[i]]) * (1-b[i])); - } to } + DoubleAns = dp[n]* -; -printf"%.1lf%%\n", ans); the } * return 0; $}
The second method:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 Const intN =10005;6 intA[n];7 DoubleB[n];8 DoubleDp[n];9 DoubleMinDoubleADoubleb)Ten { One if(A<B)returnA; A Else returnb; - } - intMain () the { - intN, M; - while(~SCANF ("%d%d",&n,&m)) - { + if(n==0&&m==0)return 0; - for(inti =0; i < N; i++) +Dp[i] =1; A for(inti =0; I < m; i++) atscanf"%D%LF",&a[i],&b[i]); - for(inti =0; I < m; i++) - { - for(intj = N; J >= A[i]; j--) - { -Dp[j] = min (dp[j],dp[j-a[i]]* (1-b[i])); in } - } to DoubleAns = (1-dp[n]) * -; +printf"%.1lf%%\n", ans); - } the return 0; *}
0/1 Backpack DP Learning