HDU 1203 I NEED A OFFER! 动态规划01背包
http://acm.hdu.edu.cn/showproblem.php?pid=1203
题意:
一道01背包的水题,每一行分别是申请所需的费用和被录取的概率,Speakless只有一定的钱,要在这些规定的钱内找到至少能被一所学校
录取的概率的最大值。
坑爹:
才刚开始看01背包问题,对初始化的的操作不是很清楚,而且这个在计算f[n]的时候要尽量的小,而达到1-f[n]最大。
解法:
套用01背包的公式,而且将 f 数组全部初始化为1,从而保证没有一所学校录取的时候 1 - f [n] 为 0.
View Code
1 #include<iostream> 2 using namespace std; 3 4 const int maxn = 100000 +10; 5 6 struct node 7 { 8 int price; 9 double chance; 10 }; 11 struct node offer[maxn]; 12 double f[maxn]; 13 int n; 14 int m; 15 16 void ZeroOnePack(int cost,double weight) 17 { 18 int j; 19 for(j=n; j>=cost; j--) 20 { 21 if(f[j]>f[j-cost]*weight) 22 { 23 f[j]=f[j-cost]*weight; 24 } 25 } 26 } 27 28 int main() 29 { 30 while(cin>>n>>m,n+m) 31 { 32 int i; 33 for(i=0; i<=n; i++) 34 { 35 f[i]=1.0; 36 } 37 for(i=1; i<=m; i++) 38 { 39 cin>>offer[i].price>>offer[i].chance; 40 offer[i].chance=1.0-offer[i].chance; 41 } 42 for(i=1; i<=m; i++) 43 { 44 ZeroOnePack(offer[i].price,offer[i].chance); 45 } 46 printf("%.1lf%%\n",(1.0-f[n])*100); 47 } 48 return 0; 49 }