HDU - 2955 Robberies(01背包)
题意:给出N个银行拥有的资产和偷该银行被抓的概率,求在最终被抓概率小于P的情况下,能偷走的最大资产。
分析:
1、成功逃跑才能偷走财产,且每一次偷都要成功逃跑最终才能计算偷走的最大资产,即该问题可转化为求最大的逃跑概率。
2、参见:http://www.cnblogs.com/tyty-Somnuspoppy/p/6919268.html的思想:
可得状态转移方程:dp[j] = max(dp[j], dp[j - m[i]] * (1 - p[i]));---如果不偷当前银行,自然不会被抓,逃跑概率为1。
dp[j]---截止到当前银行,总共偷走j资产的情况下最大的逃跑概率。
3、最后,逆序枚举偷走的资产,一旦被抓概率小于P,即为答案。
#include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define lowbit(x) (x & (-x)) const double eps = 1e-8; inline int dcmp(double a, double b){ if(fabs(a - b) < eps) return 0; return a > b ? 1 : -1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const int MAXN = 100 + 10; const int MAXT = 10000 + 10; using namespace std; int m[MAXN]; double p[MAXN]; double dp[MAXT]; int main(){ int T; scanf("%d", &T); while(T--){ memset(dp, 0, sizeof dp); double P; int N; scanf("%lf%d", &P, &N); int sum = 0; for(int i = 1; i <= N; ++i){ scanf("%d%lf", &m[i], &p[i]); sum += m[i]; } dp[0] = 1; for(int i = 1; i <= N; ++i){ for(int j = sum; j >= m[i]; --j){ dp[j] = max(dp[j], dp[j - m[i]] * (1 - p[i])); } } for(int i = sum; i >= 0; --i){ if(1 - dp[i] < P){ printf("%d\n", i); break; } } } return 0; }