1203 I NEED A OFFER!

来源:互联网 发布:golang sleep 1毫秒 编辑:程序博客网 时间:2024/05/17 22:26


http://acm.hdu.edu.cn/showproblem.php?pid=1203

求能得到的最大概率,求概率一般都是求它的反面,由反面推出正面
#include<iostream>#include<cstring>#include<algorithm>using namespace std;struct Node {    int mny;    double proba;    double value;  /*value 为 mny与proba的比值,越低越好*/}offer[10001];int cmp(const Node &a,const Node &b){    if(a.value < b.value)        return 1;    else if(a.value == b.value)        return a.mny <b.mny?1:0;    else return 0;}int m,n;int main(){    int i;    while(scanf("%d%d",&m,&n) !=EOF &&(m||n))    {        for( i=0;i<n;i++)        {            scanf("%d%lf",&offer[i].mny,&offer[i].proba);            offer[i].value  = (offer[i].mny +0.0) / offer[i].proba;        }        sort(offer,offer+n,cmp);        double x=1;        int    sum = 0;        for(i=0;i<n;i++)        {            sum += offer[i].mny;            if(sum > m)                break;            x *= (1-offer[i].proba);        }        printf("%.1lf%%\n",(1-x)*100);                }    return 0;    }
另外一个算法 
 #include<stdio.h> double min(double x,double y) {     return x > y ? y : x; } int main() {      int n,m,i,j;      int a[1001];      double p[1001],dp[10001];      while( scanf("%d%d",&n, &m ), n||m )      {           for( i=0; i<m; i++)           {                scanf("%d%lf",&a[i],&p[i]);                p[i]=1.0-p[i];           }           for( i=0; i<=n; i++ )                dp[i]=1.0;           for( i=0; i<m; i++ )             for( j=n; j>=a[i]; j-- )                 dp[j] = min(dp[j],dp[j-a[i]]*p[i]);           printf("%.1lf%%\n",(1-dp[n])*100);      }      return 0; }