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

Topic links: http://acm.hdu.edu.cn/showproblem.php?pid=1203
meaning of the questions:
hands $ n, there is likely to have a university admission m, the application fee is a, the probability of admission to b. Let us seek to get at least one offer of maximum probability.
Solution:
seeking the maximum probability of at least one offer, we can convert a first order to an offer are not the smallest probability.
dp [i] is the probability that i kept at a minimum application fee will not offer, where the state transition equation is the multiplication, and then here, because it is seeking the probability (real data), so we dp [] on to all initialized to 1, ans also initialized to 1.
There is also a special case to note is: n is 0, but there is also an application fee of 0 for the school, which will deal with the following special on it.
Code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <list>
#include <map>
#define P(x) x>0?x:0
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=1e4+5;
const int maxm=1e4+5;

int n,m;
double dp[maxn];
double ans;
struct node
{
    int a;
    double b;
    node(int aa=0,double bb=0):a(aa),b(bb){}
    friend bool operator < (node n1,node n2)
    {
        return n1.a<n2.a;
    }
}item[maxm];

void init()
{
    for(int i=0;i<maxn;i++)
    {
        dp[i]=1;
    }
    ans=1;
}

int main()
{
    while(~scanf("%d%d",&n,&m)&&(n||m))
    {
        init();
        for(int i=1;i<=m;i++)
        {
            scanf("%d%lf",&item[i].a,&item[i].b);
            item[i].b=1-item[i].b;//我们让item[i].b存不会被录取的概率
        }
        if(n==0)
        {
            for(int i=1;i<=m;i++)
            {
                if(item[i].a==0)
                    ans*=item[i].b;
            }
            printf("%.1f%%\n",(1-ans)*100);
            continue;
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=n;j>=item[i].a;j--)
            {
                dp[j]=min(dp[j-item[i].a]*item[i].b,dp[j]);
            }
        }
        for(int i=1;i<=n;i++)
            ans=min(ans,dp[i]);
        printf("%.1f%%\n",(1-ans)*100);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44049850/article/details/94722596