这道题目我也是无语了,自己跟智障一样。非常明显的01背包题目,但是要注意的是这个概率是相乘出来得到的逃脱概率而不是直接相加的,同时最后要注意精度,我也是看了眼界,就是因为这个WA3次。
题目链接:https://vjudge.net/problem/HDU-2955
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 105;
double sum;
int cntn , s;
double dp[10005];
struct Bank {
double risk;
int money;
Bank (double a=0 , int b=0) : risk(a),money(b) {}
}bank[maxn];
void solve () {
for (int i = 1 ; i <= s ; i++)
dp[i] = -1000000;
dp[0] = 1;
for (int i = 1 ; i <= cntn ; i++) {
for (int j = s ; j >= bank[i].money ; j--)
dp[j] = max(dp[j] , dp[j-bank[i].money]*bank[i].risk);
}
for (int i = s ; i >= 0 ; i--)
if (dp[i] - (1-sum) > 0.000000001) {
printf("%d\n", i);
break;
}
}
int main () {
int ncase;
scanf("%d" , &ncase);
while (ncase--) {
memset(bank, 0, sizeof(bank));
s = 0;
scanf("%lf %d" , &sum , &cntn);
for (int i = 1 ; i <= cntn ; i++) {
double risk;
int money;
scanf("%d %lf" , &money , &risk);
bank[i] = Bank(1-risk , money);
s += money;
}
solve();
}
}