hdu 2955
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2955
题意:抢银行,给出每一个银行的钱和被抓的概率,被抓的总概率低于p才能顺利逃脱,求最多偷多少钱。
mark:01背包的转换,需要把偷的钱当作w来考虑。
代码:
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <iostream> #include <algorithm> #include <map> #define LL long long using namespace std; const int M = 10010; const int N = 110; const double eps = 1e-9; double p; int n,vv; int v[N]; double w[N],dp[M]; void zero_dp(int i) { for(int j = vv; j >= v[i]; j--) dp[j] = max(dp[j], dp[j-v[i]]*(1-w[i])); } int main() { int t; scanf("%d", &t); while(t-- && scanf("%lf%d", &p, &n)) { vv = 0; for(int i = 0; i < n; i++) { scanf("%d%lf", v+i, w+i); vv += v[i]; } memset(dp, 0, sizeof(dp)); dp[0] = 1; for(int i = 0; i < n; i++) zero_dp(i); for(int i = vv; i >= 0; i--) if(dp[i] >= 1-p) {printf("%d\n", i); break;} } return 0; }