【HDU 1203】 I NEED A OFFER!(01背包)

I NEED A OFFER!

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768

Problem Description

Speakless wanted to go abroad for a long time, and now he has finished all the required exams and prepared all the materials to be prepared, so he needs to apply to the school. To apply to any university abroad, you have to pay a certain application fee, which is amazing. Speakless didn't have much money, and only saved $n million in total. He will choose a number of m schools (of course within his financial range). Each school has a different application fee a (ten thousand U.S. dollars), and Speakless estimated the probability that he will get an offer from this school b. Whether different schools get offers will not affect each other. "I NEED A OFFER", he shouted. Help this poor man, help him calculate the maximum probability that he can receive at least one offer. (If Speakless chooses multiple schools, you can get an offer from any school).

Input

There are several sets of data input, the first row of each set of data has two positive integers n, m (0<=n<=10000,0<=m<=10000)
and the m rows behind, each row has two data ai (integer type) and bi (real type) respectively represent the application fee of the i-th school and the probability of getting an offer.
There are two zeros at the end of the input.

Output

Each set of data corresponds to an output, which represents the maximum probability that Speakless may get at least one offer. Expressed as a percentage, accurate to one 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 ‘%’.

If you haven’t learned a backpack, please refer to the ninth lecture on backpacking .
http://www.cnblogs.com/jbelial/articles/2116074.html
This topic is obviously that we follow the previous order and find that the probability of the next time is related to the previous time. So we don't ask for the probability of him getting the job but calculate the probability that he can't get the job.
dp[j] = min( dp[j],dp[jw[i]]*v[i] )[state transition equation]

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"

using namespace std;

const int maxn = 1e4+5;
double dp[maxn];
int w[maxn];
double v[maxn];
int n,m;

 int main(){
    while( ~scanf("%d%d",&n,&m) ){
        if(n==0&&m==0) break;
        for( int i=1 ; i<=m ; i++ ){
            scanf("%d%lf",&w[i],&v[i]);
            v[i]=1-v[i];
         }
        for( int i=0 ; i<=n ; i++ ){
            dp[i] = 1;
        }
        for( int i=1 ; i<=m ; i++ ){
            for( int j=n ; j>=w[i] ; j-- ){
                dp[j] = min( dp[j],dp[j-w[i]]*v[i] );
            }
        }
        printf("%.1lf%%\n",(1-dp[n])*100);
     }
     return 0;
 }

Guess you like

Origin blog.csdn.net/thesprit/article/details/52046773