dp 2955

#include<cstdio>
#include<iostream>
#include<string.h>
using namespace std;
struct node
{
int money;
double p;
}bag[101];
double maxx(double x,double y)
{
if(x>y) return x;
else return y;
}
int main()
{
int i,j,t,n,sum;
double dp[10010],max;
cin>>t;
while(t--)
{
cin>>max>>n;
sum=0;
for(i=1;i<=n;i++)
{
cin>>bag[i].money>>bag[i].p;
sum+=bag[i].money;
}
memset(dp,0,sizeof(dp));
dp[0]=1;
for(i=1;i<=n;i++)
{
for(j=sum;j>=bag[i].money;j--)
{
dp[j]=maxx(dp[j],dp[j-bag[i].money]*(1-bag[i].p));
}
}
for(i=sum;i>=0;i--)
{
if(dp[i]>(1-max))
{
cout<<i<<endl;
break;
}
}
}
return 0;
}

这道是道简单的01包问题 但是 如果用概率做背包容量的话 太复杂 这里用money做背包容量  在拿到最多钱的同时 避免被抓 最后的

for(i=sum;i>=0;i--)
{
if(dp[i]>(1-max))
{
cout<<i<<endl;
break;
}

这段用来确定出最后的结果(这里要回忆一下01背包的表格——即子问题重叠 不断的更新)

时间: 2024-12-29 10:10:29

dp 2955的相关文章

HDU 2955 Robberies dp +背包

题目链接~~http://acm.hdu.edu.cn/showproblem.php?pid=2955 题意 : 在不被抓住的情况下,偷的钱最多, 然后题目给的是被抓住的概率~ 就可以考虑在不被抓住的情况下,拿的最多的钱.. 还RT了一回    %>_<% 状态方程: dp[j]=max(dp[j],dp[j-a[i]]*p1[i]); 代码:: 1 #include <iostream> 2 #include <cstdio> 3 #include <cmat

POJ 2955:Brackets(区间DP)

http://poj.org/problem?id=2955 题意:给出一串字符,求括号匹配的数最多是多少. 思路:区间DP. 对于每个枚举的区间边界,如果两边可以配对成括号,那么dp[i][j] = dp[i+1][j-1] + 2,表示由上一个状态加上当前的贡献. 然后和普通的区间合并一样去更新. 1 #include <cstring> 2 #include <cstdio> 3 #include <iostream> 4 #include <string&

(背包dp)HDU - 2955 Robberies

原题链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2955 (可能失效) 题意:小偷抢劫n个银行,每个银行有两个属性,m为拥有的库存金额,p为小偷偷这个银行被抓的概率.小偷抢银行的总被抓概率不能超过V.求最多抢到的金额. 分析:这题拿到手很容易想到把概率*100,以总概率V为背包容积,以抢到的金额为价值跑01背包. 但是确实本题坑之所在.WA了好多发. 看了大神博客才明白,这里需要一个转化,直接跑以总金额sum为容积,不被抓的概率为价值的背

POJ 2955 Brackets (区间dp 括号匹配)

Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3951   Accepted: 2078 Description We give the following inductive definition of a "regular brackets" sequence: the empty sequence is a regular brackets sequence, if s is a reg

poj 2955 Brackets【区间DP】

题目链接:http://poj.org/problem?id=2955 题意:求回文子串的最大长度. 解法:枚举区间长度,更新答案. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string> #include <functional> #include

HDU 2955 Robberies 背包概率DP

A - Robberies Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 2955 Appoint description: Description The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usu

HDU 2955 Robberies(DP)

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=2955 题目: Problem Description The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually gets caught in the end, often because they become too greedy. He has decide

poj 2955 Brackets dp简单题

//poj 2955 //sep9 #include <iostream> using namespace std; char s[128]; int dp[128][128]; int n; int rec(int l,int r) { if(dp[l][r]!=-1) return dp[l][r]; if(l==r) return dp[l][r]=0; if(l+1==r){ if(s[l]=='('&&s[r]==')') return dp[l][r]=2; if(

(区间dp 或 记忆化搜素 )Brackets -- POJ -- 2955

http://poj.org/problem?id=2955 Description We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a regular brackets sequence, if s is a regular brackets sequence, then (s) and [s] are regular brackets sequ