offer (knapsack problem, DP)

 

Jun garlic 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. Garlic king did not have much money, we saved a total of only 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 garlic king 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 select multiple garlic Jun school, get a school of any offer can be).

Input Format

The first line has two positive integers n, m (0≤n≤10000,0≤m≤10000).

Behind the m rows, each row has two data A i (integer), B i (real) denote the i-th application fee and schools may offer to get the probability.

Output Format

Each set of data corresponds to an output representing Jun garlic may offer the greatest probability of at least one. Expressed as a percentage, accurate to one decimal place.

Sample input

10 3
4 0.1
4 0.2
5 0.3

Sample Output

44.0%

 

01 knapsack problem

Here we change the status of the state transition equation will be 01 and in front of the backpack transfer equation.

dp [j] represents the cost j yuan, at least you can get the probability offer of a school.

When there is a probability that the school is spent x dollars to get the offer is y. So this time is 1.0- (1- dp [jx]) * (1-y), calculates a first probability that pass the test, then its complement that is admitted to the probability of at least a school.

State transition equation:

dp[j]= max(dp[j],1.0-(1- dp[j-x])*(1- y))

 

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 #include <ctime>
14 const int INF=0x3f3f3f3f;
15 typedef long long LL;
16 const int mod=1e9+9;
17 const LL MOD=1e9+7;
18 const double PI = acos(-1);
19 const double eps =1e-8;
20 #define Bug cout<<"---------------------"<<endl
21 const int maxn=1e5+10;
22 using namespace std;
23 
24 int a[10005];
25 double b[10005];
26 double dp[10005];
27  
28 int main()
29 {
30     #ifdef DEBUG
31     freopen("sample.txt","r",stdin);
32     #endif
33 //    ios_base::sync_with_stdio(false);
34 //    cin.tie(NULL);
35     
36     int n,m;
37     scanf("%d %d",&n,&m);
38     for(int i=1;i<=m;i++)
39         scanf("%d %lf",&a[i],&b[i]);
40     for(int i=1;i<=m;i++)
41     {
42         for(int j=n;j>=a[i];j--)
43             dp[j]=max(dp[j],1.0-(1-dp[j-a[i]])*(1-b[i]));
44     }
45     printf("%.1f%%\n",dp[n]*100);
46     
47     return 0;
48 }

 

 

 

 

 

 

-

Guess you like

Origin www.cnblogs.com/jiamian/p/12218841.html