4-5 hdu I NEED A OFFER!

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. 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%

Ideas:

Transformation knapsack problem. It is entitled to receive at least the possibility of a job. In other words there is no possibility of that is to work.
So that it is, before i get to the school, not all probability school admission. School enrollment is not recorded, is recorded and not recorded.
Before the original knapsack problem is with

for ( int i = 1 ; i <= 数目 ; i ++ )  //01背包
	for ( int j = 第 i 个背包的重量 ; j <= 可有总重 ;j ++ )
		dp [ j ] = max ( dp [ j ] , dp [ j - 第i个背包重量 ] + 第 i 个背包的价值 ) ;
		
for ( int i = 1 ; i <= 数目 ; i ++ )  //完全背包
	for ( int j = 可有总重 ; j 》= 第 i 个背包的重量 ;j -- )
		dp [ j ] = max ( dp [ j ] , dp [ j - 第i个背包重量 ] + 第 i 个背包的价值 ) ;

But these can not be used in probability they found inside.

Now is not the probability of being admitted before the minimum, the minimum probability now available.

//题目并没有说一所学校只能申请一次。
for ( int i = 0 ; i <= 数量 ; i ++ )
	for ( int j = 拥有的资产 ; j >= 第 i 所学校所需资金 ; j-- )
		dp [ j ] = min ( dp [ j ] , dp [ j - 第 i 所学校所需资金 ] * ( 1 - 第 i 所学校被录取概率 ) ;
//  ( 1 - 第 i 所学校被录取概率 ) 表示不被录取的概率。

AC Code:

#include<bits/stdc++.h>
using namespace std;

const int inf=0x3f3f3f;
int n,m;
int a[10005];
double b[10005];
double dp[10005];

int main(){
	while(cin>>n>>m){
		if(n==0&&m==0){
			break;
		}
		for(int i=0;i<m;i++){//输入数据 
			cin>>a[i]>>b[i];
		}
		for(int i=0;i<=n;i++){//dp数组归1 
			dp[i]=1;
		}
		for(int i=0;i<m;i++){ 
			for(int j=n;j>=a[i];j--){
				dp[j]=min(dp[j],dp[j-a[i]]*(1-b[i]));
			}
		}
		printf("%.1lf%%\n",(1-dp[n])*100);
	}
	return 0;
}

This problem has greatly improved on the knapsack problem.

Published 34 original articles · won praise 6 · views 1331

Guess you like

Origin blog.csdn.net/qq_44669377/article/details/105325265