【LightOJ - 1079】Just another Robbery(概率dp,概率背包)

题干:

As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But his friends - Hermione and Ron have decided upon a tolerable probability P of getting caught. They feel that he is safe enough if the banks he robs together give a probability less than P.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a real number P, the probability Harry needs to be below, and an integer N (0 < N ≤ 100), the number of banks he has plans for. Then follow N lines, where line j gives an integer Mj (0 < Mj ≤ 100) and a real number Pj . Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj. A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent as the police have very low funds.

Output

For each case, print the case number and the maximum number of millions he can expect to get while the probability of getting caught is less than P.

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

Case 1: 2

Case 2: 4

Case 3: 6

Note

For the first case, if he wants to rob bank 1 and 2, then the probability of getting caught is 0.02 + (1 - 0.02) * .03 = 0.0494 which is greater than the given probability (0.04). That's why he has only option, just to rob rank 2.

题目大意:

有n个银行,每个银行有v[i]的钱,和被抓住的概率p[i]。你要按一定顺序去抢,要求你被抓住的概率小于等于P,你最多能获得多少钱。 

解题报告:

首先不难证明,假设你选择了其中某num个银行,那么与选择的银行的顺序无关。证明如下:(假设你选择了第i个和的第j个)

考虑被抓的概率:p_i + (1-p_i)\times p_j == p_j + (1-p_j)\times p_i,所以是等效的。

或者考虑不被抓的概率:(1-p_i) \times (1-p_j) == (1-p_j) \times (1-p_i),所以是等效的。

设定状态dp[i][j]代表前i个银行,得到了j块钱但是不被抓到最大概率。(其实设置最小概率应该也可以,但是转移的时候不是很好写。)

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 10000 + 5;
double dp[205][MAX],P,p[205];
int n,v[205];
int main()
{int t,iCase=0;cin>>t;while(t--) {scanf("%lf%d",&P,&n);int sum = 0;for(int i = 1; i<=n; i++) scanf("%d%lf",&v[i],&p[i]),sum += v[i];for(int i = 1; i<=n; i++) {for(int j = 0; j<=sum; j++) dp[i][j]=0;}dp[0][0]=1;for(int i = 1; i<=n; i++) {for(int j = 0; j<=sum; j++) {dp[i][j] = dp[i-1][j];if(j >= v[i]) dp[i][j] = max(dp[i][j],dp[i-1][j-v[i]]*(1-p[i]));}}int ans = 0 ;for(int i = 0; i<=sum; i++) {if(dp[n][i] >= 1-P) ans = i;}printf("Case %d: %d\n",++iCase,ans);}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440055.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

强烈推荐的TensorFlow、Pytorch和Keras的样例资源(深度学习初学者必须收藏)

目录 一、TensorFlow 二、Keras 三、Pytorch 总结 本文转自微信公众号&#xff1a;机器学习初学者 原创&#xff1a; 机器学习初学者 机器学习初学者 TensorFlow、Keras和Pytorch是目前深度学习的主要框架&#xff0c;也是入门深度学习必须掌握的三大框架&#xff0c;但…

【LightOJ - 1027】A Dangerous Maze(概率dp,数学期望)

题干&#xff1a; You are in a maze; seeing n doors in front of you in beginning. You can choose any door you like. The probability for choosing a door is equal for all doors. If you choose the ith door, it can either take you back to the same position wh…

由浅到深理解ROS(2)

ROS文件系统 用户可以直接参看官网&#xff1a; http://wiki.ros.org/ROS/Tutorials/NavigatingTheFilesystem ROS文件系统中的两个最基本的概念&#xff1a;Package和Manifest&#xff0c;即包和清单文件。 &#xff08;1&#xff09;Package是组织ROS代码的最基本单位&…

Django员工管理系统

Django员工管理系统&#xff08;ems&#xff09; 需求分析&#xff1a; 1.实现管理员的注册、登陆页面注册&#xff1a;用户名、真实名字、密码、确认密码、性别、验证码用户名需要判断是否合法、是否存在&#xff0c;loading图片提示密码和确认密码是否相同&#xff0c;load…

【LightOJ - 1104】Birthday Paradox(概率,思维)

题干&#xff1a; Sometimes some mathematical results are hard to believe. One of the common problems is the birthday paradox. Suppose you are in a party where there are 23 people including you. What is the probability that at least two people in the party…

爬虫小记

中国商标网 找到正确的入口 在此页面加入全部data数据获取xhr请求包

【LightOJ - 1038】Race to 1 Again(概率dp,数学期望)

题干&#xff1a; Rimi learned a new thing about integers, which is - any positive integer greater than 1 can be divided by its divisors. So, he is now playing with this property. He selects a number N. And he calls this D. In each turn he randomly choose…

使用matplotlib进行简单的数据展示

import numpy as np import matplotlib.pyplot as plt# 解决中文乱码 plt.rcParams[font.sans-serif] [SimHei] # 用来正常显示中文标签 plt.rcParams[axes.unicode_minus] False # 用来正常显示负号# 建立一个坐标系 plt.subplot(1, 1, 1) # 指明x和y值 x np.array([1, 2…

由浅到深理解ROS(6)-坐标转换

转自 ROS 中对于多坐标系的处理是使用树型表示&#xff0c;在机器人自主导航中&#xff0c;ROS会构建这几个很重要的坐标系&#xff1a; base_link: 一般位于tf tree的最根部&#xff0c;物理语义原点一般为表示机器人中心&#xff0c;为相对机器人的本体的坐标系。(base_foot…

【BZOJ - 3450】Tyvj1952 Easy(数学期望,期望的线性性)

题干&#xff1a; 某一天WJMZBMR在打osu~~~但是他太弱逼了&#xff0c;有些地方完全靠运气:( 我们来简化一下这个游戏的规则 有n次点击要做&#xff0c;成功了就是o&#xff0c;失败了就是x&#xff0c;分数是按comb计算的&#xff0c;连续a个comb就有a*a分&#xff0c;comb就…

吐血推荐收藏的学位论文排版教程(完整版)

目录 01.保存的高级选项设置 02.纸张设置为A4纸大小 03.设置页边距和装订线距离 04.度量衡的设置 05.创建“论文正文”样式 06.修改论文正文样式 07.设置并修改标题样式 08.给中英文摘要、附录、等大标题套用样式 09.两个Word文档之间进行并排查看 12.一键生成多级列…

由浅到深理解ROS(7)-URDF

转自 相信许多爱好者止步于昂贵的机器人价格。虽然有了Arduino&#xff0c;但一个载重能力很弱的机器人小车成本也得一两百块钱。搭建自己的机器人更是费时费力。 所以如果你只是普通机器人爱好者&#xff0c;或者还没想好要拿机器人做什么&#xff0c;那我们还是从模拟器开始…

Apollo进阶课程 ① | 带你纵览无人车

目录 1&#xff09;无人驾驶级别的分类 2&#xff09;无人驾驶技术的关键点 3&#xff09;火热的无人驾驶 不缺独角兽 原文链接&#xff1a;Apollo进阶课程 ① | 带你纵览无人车 Apollo自动驾驶进阶课程是由百度Apollo联合北京大学共同开设的课程&#xff0c;邀请百度Apoll…

【BZOJ - 4318】OSU!(概率dp,数学期望,期望的线性性)

题干&#xff1a; osu 是一款群众喜闻乐见的休闲软件。 我们可以把osu的规则简化与改编成以下的样子: 一共有n次操作&#xff0c;每次操作只有成功与失败之分&#xff0c;成功对应1&#xff0c;失败对应0&#xff0c;n次操作对应为1个长度为n的01串。在这个串中连续的 X个…

word2vec原理详解及实战

目录 1&#xff09;前言 1.1 语言模型 1.2N-gram模型 1.3词向量表示 2&#xff09;预备知识 2.1 sigmoid函数 2.2 逻辑回归 2.3贝叶斯公式 2.4 Huffman编码 3&#xff09;神经网络概率语言模型 4&#xff09;基于Hierarchial Sodtmax模型 4.1CBOW模型 4.2 Skip-gr…

计算机网络通讯协议

网络通讯&#xff1a; 就是要把特定意义的数据通过物理介质传送给对方。把电信号变成有意义的数据&#xff1a; 以字节为单位分组&#xff0c;标识好每一组电信号的信息特征&#xff0c;按照分组的顺序来依次发送。 以太网规定&#xff1a;一组电信号为一个数据包&#xff0c…

【CodeForces - 518D】Ilya and Escalator(概率dp,数学期望)

题干&#xff1a; Ilya got tired of sports programming, left university and got a job in the subway. He was given the task to determine the escalator load factor. Lets assume that n people stand in the queue for the escalator. At each second one of the tw…

Apollo进阶课程 ② | 开源模块讲解(上)

目录 1&#xff09;无人驾驶车介绍 2&#xff09;高精地图 3&#xff09;定位 4&#xff09;感知 5&#xff09;轨迹规划 6&#xff09;控制 7&#xff09;云端 原文链接&#xff1a;Apollo进阶课程 ② | 开源模块讲解&#xff08;上&#xff09; Apollo自动驾驶进阶课…

由浅到深理解ROS(9)- 几个基本概念的理解 坐标系 包

1.坐标系 最常用的就是map&#xff0c;odom&#xff0c;base_link&#xff0c;base_laser坐标系&#xff0c;这也是开始接触gmapping的一些坐标系。 map:地图坐标系&#xff0c;顾名思义&#xff0c;一般设该坐标系为固定坐标系&#xff08;fixed frame&#xff09;&#xff…

【POJ - 1698】Alice's Chance(网络流最大流,建图)

题干&#xff1a; Alice, a charming girl, have been dreaming of being a movie star for long. Her chances will come now, for several filmmaking companies invite her to play the chief role in their new films. Unfortunately, all these companies will start mak…