Passage

时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte
总提交: 40 测试通过: 20

描述

Bill is a millionaire. But
unfortunately he was trapped in a castle. There are only n passages to go out.
For any passage i (1<=i<=n), Pi (0<=Pi<=1) denotes the probability
that Bill will escape from this castle safely if he chose this passage. Qi
(0<=Qi<=1-Pi) denotes the probability that there is a group of guards in
this passage. And Bill should give them one million dollars and go back.
Otherwise, he will be killed. The probability of this passage had a dead end is
1-Pi-Qi. In this case Bill has to go back. Whenever he came back, he can choose
another passage.
We already know that Bill has M million dollars. Help Bill
to find out the probability that he can escape from this castle if he chose the
optimal strategy.

输入

The first line contains an
integer T (T<=100) indicating the number of test cases.
The first line of
each test case contains two integers n (1<=n<=1000) and M
(0<=M<=10).
Then n lines follows, each line contains two float number
Pi and Qi.

输出

For each test case, print the
case number and the answer in a single line.
The answer should be rounded to
five digits after the decimal point.
Follow the format of the sample
output.

样例输入

3
1 10
0.5 0
2 0
0.3 0.4
0.4 0.5
3 0
0.333 0.234
0.353 0.453
0.342 0.532

样例输出

Case 1: 0.50000
Case 2: 0.43000
Case 3: 0.51458

题目来源

“光庭杯”第五届华中北区程序设计邀请赛 暨 WHU第八届程序设计竞赛

如果有两总选择 P1, Q1 and P2, Q2

先选第一条路被抓的概率为 ret1 = (1 - p1 - Q1) * Q2 + Q1

先选第二条路被抓的概率为 ret2 = (1 - p2 - Q2) * Q1 + Q2

设P1 / Q1 > P2 / Q2  => P1 * Q2 > P2 * Q1

ret1 - ret2 =  P2 * Q1 - P1 * Q2 < 0

所以优先选择第一条路

先按Pi / Qi 从大到小排序

dp[i][j] 表示到第 i 条路, 消耗 j million dollars的概率

逃跑的概率 ret += dp[i-1][j] * Pi ( j <= k )

dp[i][j] = dp[i-1][j] * (1.0 - Pi - Qi) + dp[i-1][j-1] * Qi

 #include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std; struct Node{
double Pi, Qi;
}; bool cmp(Node a, Node b){
return a.Pi / a.Qi > b.Pi / b.Qi;
} Node path[];
double dp[][];
double deal(int n, int k){
memset(dp, , sizeof(dp));
dp[][] = ;
double ret = ;
for(int i = ; i <= n; i++){ for(int j = k; j >= ; j--){
ret += dp[i-][j] * path[i].Pi;
dp[i][j] += dp[i-][j] * (1.0 - path[i].Pi - path[i].Qi);
dp[i][j] += dp[i-][j-] * path[i].Qi;
}
ret += dp[i-][] * path[i].Pi;
dp[i][] = dp[i-][] * (1.0 - path[i].Pi - path[i].Qi);
}
return ret;
} int main()
{
int T;
scanf("%d", &T);
for(int cas = ; cas <= T; cas++){
int n, k;
scanf("%d %d", &n, &k);
for(int i = ; i <= n; i++){
scanf("%lf %lf", &path[i].Pi, &path[i].Qi);
}
sort(path+, path+n+, cmp);
printf("Case %d: %.5lf\n", cas, deal(n, k));
}
return ;
}

toj 3086 Passage (不错)的更多相关文章

  1. 在知乎上看到 Web Socket这篇文章讲得确实挺好,从头看到尾都非常形象生动,一口气看完,没有半点模糊,非常不错

    在知乎上看到这篇文章讲得确实挺好,从头看到尾都非常形象生动,一口气看完,没有半点模糊,非常不错,所以推荐给大家,非常值得一读. 作者:Ovear链接:https://www.zhihu.com/que ...

  2. Coursera上一个不错的Java课

    地址:https://www.coursera.org/learn/java-chengxu-sheji/home/welcome 复习天昏地暗,看点视频调剂一下.发现这个讲的还是很不错的.北大毕竟比 ...

  3. 三款不错的图片压缩上传插件(webuploader+localResizeIMG4+LUploader)

    涉及到网页图片的交互,少不了图片的压缩上传,相关的插件有很多,相信大家都有用过,这里我就推荐三款,至于好处就仁者见仁喽: 1.名气最高的WebUploader,由Baidu FEX 团队开发,以H5为 ...

  4. 一个不错的vue表单验证插件

    github文档 用着不错,官方的文档例子很简单 <body> <div id="app"> <validator name="valida ...

  5. 推荐一个不错的css3网站 可以直接调用的

    animate.css 一搜就能出来  我用着还不错

  6. 关于移动app开发的一些不错的站点

    1. http://www.androiddevtools.cn      Android Dev Tools官网地址:www.androiddevtools.cn 收集整理Android开发所需的A ...

  7. TOJ 2776 CD Making

    TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性...  贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...

  8. 推荐一些不错的计算机书籍(php c mysql linux等等)

    推荐一些不错的计算机书籍. # PHP<PHP程序设计>(第2版)  --PHP语法和入门最好的书<PHP5权威编程>  --PHP入门后升级书<深入PHP:面向对象.模 ...

  9. DSL 或者说是抽象 或者说是沉淀 ,我看到的不错的一篇文章

    作者:张浩斌 链接:https://www.zhihu.com/question/45552115/answer/99388265 来源:知乎 著作权归作者张浩斌和知乎所有.   ---------- ...

随机推荐

  1. Ubuntu18.04 安装配置mongodb

    一.安装 # 1. 更新 sudo apt-get update # 2. 安装 sudo apt-get install -y mongodb # 3. 查看是否安装成功 # a. 服务状态 sud ...

  2. NOI2.5 1490:A Knight's Journey

    描述 Background The knight is getting bored of seeing the same black and white squares again and again ...

  3. C#的WinForm窗体美化

    为了帮助用户追求美观,.NET 4.0 专门为对此有需求的人提供了IrisSkin4.dll皮肤引用集,里面封装了许多对窗体重新描绘的方法,再搭配上WinForm特有的 .ssk 文件,就可以实现窗体 ...

  4. Redis(七):set/sadd/sismember/sinter/sdiffstore 命令源码解析

    上两篇我们讲了hash和list数据类型相关的主要实现方法,同时加上前面对框架服务和string相关的功能介绍,已揭开了大部分redis的实用面纱. 现在还剩下两种数据类型: set, zset. 本 ...

  5. Mac系统 python2.7中安装MySQLdb

    由于要在python2.7上使用到MySQLdb连接数据库,所以要安装MySQLdb,也就是MySQL-Python.安装之前已经有人告诉我,这个东西比较难装,果然我也遇到好多问题,在百度找了半天,发 ...

  6. ssh隧道使用

    在内网中几乎所有的linux服务器和网络设备都支持ssh协议.一般情况下,ssh协议是允许通过防火墙和边界设备的,所以经常被攻击者利用.同时ssh协议的传输过程是加密的,所以我们很难区分合法的ssh会 ...

  7. Leetcode 题目整理-3 Palindrome Number & Roman to Integer

    9. Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. clic ...

  8. 深入了解 Java 中的异常处理 + 面试题

    # 深入了解 Java 中的异常处理 + 面试题 在程序开发中,异常处理也是我们经常使用到的模块,只是平常很少去深究异常模块的一些知识点.比如,try-catch 处理要遵循的原则是什么,finall ...

  9. 微信小程序 客户端时间 与 服务端时间

    服务端时间 db.serverDate(); 在操作数据库,上传数据的时候可以使用服务端时间 wx.cloud.init();//初始化云 const db = wx.cloud.database() ...

  10. axure如何实现提示框3s后自动消失

    本示例基于axure8 实现 1.先做两个元件,一个按钮,一个提示框 2.将弹框“发布成功提示”设置为,页面载入时隐藏,这样预览页面时,该弹框是隐藏状态 3.给按钮添加交互样式,如下: 4.预览,点击 ...