HDU 2955 Robberies

1.题目描写叙述:点击打开链接

2.解题思路:本题利用01背包解决。只是略微运用了一下逆向思维。

假设依照经典的思路,应该是概率作为容量,钱数作为价值,可是因为概率是浮点数,不能直接当做下标来使用。因此最好还是换一个角度来考虑:概率作为价值。钱数作为容量。

我们把全部的概率都转化为不被抓的概率,那么,本题实际上是求解不被抓的概率刚刚大于P的时候,最大的容量是多少。

这样就能够用经典的01背包求解了。

3.代码:

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<algorithm>
#include<cassert>
#include<string>
#include<sstream>
#include<set>
#include<bitset>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cctype>
#include<functional>
using namespace std;

#define me(s)  memset(s,0,sizeof(s))
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair <int, int> P;

#define rep(i,n) for(int i=0;i<(n);i++)

const int N=10000+5;
const double eps=1e-8;
int w[N];
double p[N],not_catch[N];

int main()
{
    int T,n;
    double P;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lf%d",&P,&n);
        me(not_catch);
        P=1-P;
        int sum=0;
        not_catch[0]=1.0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%lf",&w[i],&p[i]);
            sum+=w[i];
            p[i]=1.0-p[i];
        }
        for(int i=0;i<n;i++)
            for(int j=sum;j>=w[i];j--)
                not_catch[j]=max(not_catch[j],not_catch[j-w[i]]*p[i]);
        for(int i=sum;i>=0;i--)
            if(not_catch[i]-P>eps)
        {
            printf("%d
",i);break;
        }
    }
}



原文地址:https://www.cnblogs.com/clnchanpin/p/7258662.html