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. ARCproject中加入非ARC文件,或者非ARC环境中加入ARC文件

    ARC与非ARC在一个项目中同一时候使用, 选择项目中的Targets,选中你所要操作的Target,选Build Phases,在当中Complie Sources中选择须要ARC的文件双击,并在输 ...

  2. JDBC学生管理系统--处理分页显示

    分页的思想: 假设一共有104条数据,每页显示10条数据: select * from student limit 0,10; 页数是index,第index页,对应的sql语句是: select * ...

  3. HDU2024JAVA

    C语言合法标识符 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  4. NDK开发之访问域

    Java有两类域,实例域和静态域.类的每个实例都有自己的实例域副本,而一个类的所有实例共享一个静态域(Java SE基础). JNI提供了相应的函数来访问这两类域,总体步骤是这样的: 1.通过对象引用 ...

  5. Android开发之TextView的下划线添加

    如何给TextView添加下划线呢,最近项目中需要这个,于是就用代码添加了下划线功能.主要就是用Paint的setFlags方法来实现,具体如下: ((TextView)mScrollView.fin ...

  6. XC图片浏览器

    XC图片浏览器,这是基于Android4.4开发的一款浏览手机里的图片的浏览器.简单美观实用.欢迎下载. 下载地址:http://download.csdn.net/detail/jczmdevelo ...

  7. Java基础知识强化之集合框架笔记53:Map集合之Map集合的遍历 键值对对象找键和值

    1. Map集合的遍历(键值对对象找键和值) Map -- 夫妻对  思路:  A: 获取所有结婚证的集合  B: 遍历结婚证的集合,得到每一个结婚证  C: 根据结婚证获取丈夫和妻子 转换:  A: ...

  8. SQL语句中格式化时间

    给数据库中的字段格式化(): to_char(CREATETIME,'yyyy-MM-dd') 给程序中的字段格式化(InTime为数据库字段): InTime = to_date( '" ...

  9. mvc5 + ef6 + autofac搭建项目(repository+uow)(一)

    直奔主题了,不那么啰嗦. 整体框架的参考来源是  O# 的框架,在此感谢锋哥一直以来的开源,让我们有的学 如下图: (图一) 一下分三个步骤说明,分别为 dbContext,repository,uo ...

  10. 跨域的小小总结:js跨域及跨域的几种解决方法

    一.什么是跨域?? js跨域请求就是使用js访问iframe里的不同域名下的页面内容,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同的域的iframe框架中的数据.即只要域名.协议. ...