Robberies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12161    Accepted Submission(s): 4527

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 decided to work in the lucrative business of bank robbery only for a short while, before retiring to a comfortable job at a university.


For a few months now, Roy has been assessing the security of various banks and the amount of cash they hold. He wants to make a calculated risk, and grab as much money as possible.

His mother, Ola, has decided upon a tolerable probability of getting caught. She feels that he is safe enough if the banks he robs together give a probability less than this.

 
Input
The first line of input gives T, the number of cases. For each scenario, the first line of input gives a floating point number P, the probability Roy needs to be below, and an integer N, the number of banks he has plans for. Then follow N lines, where line j gives an integer Mj and a floating point number Pj . 
Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj .
 
Output
For each test case, output a line with the maximum number of millions he can expect to get while the probability of getting caught is less than the limit set.

Notes and Constraints
0 < T <= 100
0.0 <= P <= 1.0
0 < N <= 100
0 < Mj <= 100
0.0 <= Pj <= 1.0
A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent as the police have very low funds.

 
Sample Input
3
0.04 3
1 0.02
2 0.03
3 0.05
0.06 3
2 0.03
2 0.03
3 0.05
0.10 3
1 0.03
2 0.02
3 0.05
 
Sample Output
2
4
6
 
Source
 
  這種時候還做揹包問題確實有點不正常,但是這道題內蘊含了一種揹包問題的思路,即在有些題目中兩個量都可以作爲揹包的下標,而需要仔細分析兩者優劣。
  這道題就是這種典型揹包問題,由於用概率×10000作爲下標會爆精度所以最佳方法爲用金錢作爲下標。
 
順便粘上兩個代碼。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
#define MAXN 10010
#define MAXV MAXN*2
#define MAXE MAXV*2
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
typedef long long qword;
inline int nextInt()
{
char ch;
int x=;
bool flag=false;
do
ch=getchar(),flag=(ch=='-')?true:flag;
while(ch<''||ch>'');
do x=x*+ch-'';
while (ch=getchar(),ch<='' && ch>='');
return x*(flag?-:);
}
double f[MAXN+];
int n,m;
int main()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int i,j,k;
int x,y,z;
int nn;
scanf("%d",&nn);
double temp;
double plim;
double p;
int tot;
int l;
int ii;
for (ii=;ii<nn;ii++)
{
scanf("%lf %d",&temp,&n);
plim=(-temp);
int ans=;
for (i=;i<=MAXN;i++)f[i]=;
f[]=;
for (i=;i<n;i++)
{
scanf("%d %lf",&tot,&p);
p=(-p);
for (j=MAXN;j>=;j--)
{
f[j+tot]=max(f[j+tot],f[j]*p);
if (f[j+tot]>plim)ans=max(ans,j+tot);
}
}
printf("%d\n",ans);
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<string>
#include<queue>
using namespace std;
#ifdef WIN32
#define LL "%I64d"
#else
#define LL "%lld"
#endif
#define MAXN 100000
#define MAXV MAXN*2
#define MAXE MAXV*2
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
typedef long long qword;
inline int nextInt()
{
char ch;
int x=;
bool flag=false;
do
ch=getchar(),flag=(ch=='-')?true:flag;
while(ch<''||ch>'');
do x=x*+ch-'';
while (ch=getchar(),ch<='' && ch>='');
return x*(flag?-:);
}
int f[MAXN+];
int n,m;
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int i,j,k;
int x,y,z;
int nn;
scanf("%d",&nn);
double temp;
int plim;
double p;
int tot;
int l;
int ii;
for (ii=;ii<nn;ii++)
{
scanf("%lf %d",&temp,&n);
plim=(-temp)*MAXN;
int ans=;
memset(f,-INF,sizeof(f));
f[MAXN]=;
for (i=;i<n;i++)
{
scanf("%d %lf",&tot,&p);
p=(-p);
for (j=plim/p;j<=MAXN;j++)
{
if (j*p<=plim)continue;
f[(int)(j*p)]=max(f[(int)(j*p)],f[j]+tot);
ans=max(ans,f[(int)(j*p)]);
}
}
printf("%d\n",ans);
}
return ;
}

hdu 2955 Robberies 背包DP的更多相关文章

  1. HDU 2955 Robberies 背包概率DP

    A - Robberies Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

  2. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  3. HDU 2955 Robberies(概率DP,01背包)题解

    题意:给出规定的最高被抓概率m,银行数量n,然后给出每个银行被抓概率和钱,问你不超过m最多能拿多少钱 思路:一道好像能直接01背包的题,但是有些不同.按照以往的逻辑,dp[i]都是代表i代价能拿的最高 ...

  4. hdu 2955 Robberies(背包DP)

    题意: 小偷去抢银行,他母亲很担心. 他母亲希望他被抓的概率真不超过P.小偷打算去抢N个银行,每个银行有两个值Mi.Pi,Mi:抢第i个银行所获得的财产 Pi:抢第i个银行被抓的概率 求最多能抢得多少 ...

  5. HDU 2955 Robberies(DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=2955 题目: Problem Description The aspiring Roy the Rob ...

  6. hdu 2955 Robberies (01背包)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 思路:一开始看急了,以为概率是直接相加的,wa了无数发,这道题目给的是被抓的概率,我们应该先求出总的 ...

  7. HDU 2955 Robberies(0-1背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=2955 题意:一个抢劫犯要去抢劫银行,给出了几家银行的资金和被抓概率,要求在被抓概率不大于给出的被抓概率的情况下, ...

  8. Hdu 2955 Robberies 0/1背包

    Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  9. hdu 2955 Robberies 0-1背包/概率初始化

    /*Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

随机推荐

  1. 使用 OpenSSL API 进行安全编程

    创建基本的安全连接和非安全连接 Kenneth Ballard ( kenneth.ballard@ptk.org), 自由程序员 Kenneth 是 Peru State College(位于 Pe ...

  2. mybatis06 增删改差 源码

    user.java package cn.itcast.mybatis.po; import java.util.Date; public class User { private int id; p ...

  3. Nginx vs Apache--reference

    May 14th, 2014 - By Walker Rowe https://anturis.com/blog/nginx-vs-apache/ What is the Nginx web and ...

  4. java 中能否使用 动态加载的类(Class.forName) 来做类型转换?

    今天同事提出了一个问题: 将对象a 转化为类型b,b 的classpath 是在配置文件中配置的,需要在运行中使用Class.forName 动态load进来,因为之前从来没有想过类似的问题,所以懵掉 ...

  5. Android 解决调用系统相册打不开图片 DecodeServices报解码错误

    这是由于系统相册不知道你图片目录是一个相册.打开前需要向系统相册“注册一下”,说白了就是让系统相册知道你这个图片所在的文件夹是个相册. private static void scanImageFil ...

  6. RedHat7/Windows7搭建JAVA开发环境(Eclipse)

    RedHat7搭建JAVA开发环境 安装JAVA # yum install java 安装Tomcat # yum install tomcat 确认Tomcat版本 # tomcat versio ...

  7. HDU-1020(水题)

    Encoding Problem Description Given a string containing only 'A' - 'Z', we could encode it using the ...

  8. react-native之站在巨人的肩膀上

    react-native之站在巨人的肩膀上 前方高能,大量图片,不过你一定会很爽.如果爽到了,请告诉我

  9. document library\ picture library\Asset Library的默认文件夹

    document library\ picture library\Asset Library的默认文件夹? document library 默认文件夹:Forms picture library默 ...

  10. [Excel] C#DataToExcel帮助类 (转载)

    点击下载 DataToExcel.rar 看下面代码吧 /// <summary> /// 类说明:DataToExcel /// 编 码 人:苏飞 /// 联系方式:361983679 ...