/*
题目描述:有n件物品,盗走第i件物品可以得到财产m[i],但同时有p[i]的概率被抓住,现要求从这n件物品中取走若干件,
使得得到的财产量最多,又要保证被抓住的概率小于P
思路:dp[i][j]表示到第i件物品时得到财产j时被抓住的概率最小值,则有
dp[i][j] = min(dp[i - 1][j] , dp[i - 1][j - m[i]] + (1 - dp[i][ j - m[i] ] ) * p[i])
min中第一项含义为从i - 1件中已经拿到了财产j,那么第i件不必冒风险去拿,所以dp[i][j] = dp[i - 1][j - 1],
第二项含义为从i-1 件中已经拿到了财产j - m[i] , 再拿走第i件就可以补全财产总量j,那么被抓到的概率等于在拿到j - m[i]
过程中被抓到的概率 + 在拿j - m[i]过程中未被抓到的概率 * 拿第i件物品时被抓到的概率
*/
#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-8;
const int maxn = 1e4 + 5;
double dp[maxn] , p[maxn];
int m[maxn];
int main()
{
int n , T , kase = 0;
double P;
scanf("%d",&T);
while(T--){
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];
}
for(int i = 0 ; i<= sum ; i++)
dp[i] = INF;
dp[0] = 0;
sum = 0;
int ans = - 1;
for(int i = 1 ; i <= n ; i++ ){
sum += m[i] ;
for(int j = sum ; j >= 1 ; j--){
if(j >= m[i] && (0 <= dp[j - m[i]]&&dp[j - m[i] ]<= 1))
dp[j] = min(dp[j - m[i]] + (1.0 - dp[j - m[i]] ) * p[i] , dp[j]);
if(dp[j] < P) ans = max(ans , j);
}
}
if(ans != -1)
printf("Case %d: %d\n" , ++kase , ans );
else
printf("Case %d: 0\n" , ++kase );
}
return 0;
}
LightOJ 1079 Just another Robbery 概率dp
最新推荐文章于 2020-11-02 21:38:44 发布