HDU 1203 I NEED A OFFER! (Dynamic programming, 01 backpacks, probability)

I NEED A OFFER!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 40874    Accepted Submission(s): 16228

 

Problem Description

 

Speakless long time I wanted to go abroad, and now he has finished all the required exams, prepare all the materials to be prepared, so they need to apply for school. To apply for any university abroad, you have to pay a fee to apply, this is very striking. Speakless not have much money, just to save a total of $ n million. He will select a number of (of course within his affordability range) in the m schools. Each school has a different application fee a (million dollars), and Speakless estimated he got the school offer the possibility of b. Whether the offer will not affect each other between different schools. "I NEED A OFFER", he cried. Help the poor folks, help him calculate that he could receive the maximum probability of at least one offer. (If you selected multiple Speakless school, get a school of any offer can be).

 


Input

 

There are several groups of data input, data of the first line of each of two positive integers n-, m (0 <= n-<= 10000,0 <= m <= 10000)
m behind the line, each line has two data ai (integer), bi (real) denote the i-th school application fee and may get the offer of probability.
Finally, there are two 0 inputs.

 


Output

 

Each set of data corresponds to an output representing Speakless possible to obtain the maximum probability of at least one offer. He expressed as a percentage, to the nearest decimal place.

 


Sample Input

10 3
4 0.1
4 0.2
5 0.3
0 0

Sample Output

44.0%

Hint

You should use printf("%%") to print a '%'.

Topic analysis

01 backpack is a simple, but be aware that the requirement that at least one school on the probability, we first calculate the probability of each are not on, and then you can then subtract 1

WA start for a long time, and finally found the input is determined not to be precise a ..... m and n are 0 job

Code

#include<bits/stdc++.h>

using namespace std;

double dp[10005],p[10005];
int i,j,s[10005],n,m;

int main()
{
    while(scanf("%d%d",&n,&m)&&(!(m==0&&n==0)))
    {
        for(i=1;i<=m;i++)
        {
            cin>>s[i]>>p[i];
        }
        for(i=0;i<=n;i++)
        dp[i]=1.0;
        for(i=1;i<=m;i++)
        {
            for(j=n;j>=s[i];j--)
            {
                dp[j]=min(dp[j],dp[j-s[i]]*(1.0-p[i]));
            }
        }
        printf("%.1lf%%\n",(1-dp[n])*100);
    }
}

 

 

Guess you like

Origin www.cnblogs.com/dyhaohaoxuexi/p/11432799.html