Codeforces 866C Gotta Go Fast - 动态规划 - 概率与期望 - 二分答案

You‘re trying to set the record on your favorite video game. The game consists of N levels, which must be completed sequentially in order to beat the game. You usually complete each level as fast as possible, but sometimes finish a level slower. Specifically, you will complete the i-th level in either Fi seconds or Si seconds, where Fi < Si, and there‘s a Pi percent chance of completing it in Fi seconds. After completing a level, you may decide to either continue the game and play the next level, or reset the game and start again from the first level. Both the decision and the action are instant.

Your goal is to complete all the levels sequentially in at most R total seconds. You want to minimize the expected amount of time playing before achieving that goal. If you continue and reset optimally, how much total time can you expect to spend playing?

Input

The first line of input contains integers N and R , the number of levels and number of seconds you want to complete the game in, respectively. N lines follow. The ith such line contains integers Fi, Si, Pi (1 ≤ Fi < Si ≤ 100, 80 ≤ Pi ≤ 99), the fast time for level i, the slow time for level i, and the probability (as a percentage) of completing level i with the fast time.

Output

Print the total expected time. Your answer must be correct within an absolute or relative error of 10 - 9.

Formally, let your answer be a, and the jury‘s answer be b. Your answer will be considered correct, if .

Examples

input

1 82 8 81

output

3.14

input

2 3020 30 803 9 85

output

31.4

input

4 31963 79 8979 97 9175 87 8875 90 83

output

314.159265358

Note

In the first example, you never need to reset. There‘s an 81% chance of completing the level in 2 seconds and a 19% chance of needing 8 seconds, both of which are within the goal time. The expected time is 0.81·2 + 0.19·8 = 3.14.

In the second example, you should reset after the first level if you complete it slowly. On average it will take 0.25 slow attempts before your first fast attempt. Then it doesn‘t matter whether you complete the second level fast or slow. The expected time is 0.25·30 + 20 + 0.85·3 + 0.15·9 = 31.4.



  题目大意 一个人打游戏,需要不超过$R$秒通过$n$关,第$i$关有$P_{i}$的概率用$F_{i}$秒通过,$\left(1 - P_{i}\right)$的概率用$S_{i}$通过($F_{i} < S_{i}$),通过每一关可以选择重置游戏,然后从头开始,或者去打下一关。问不超过$R$秒通过所有关卡的期望耗时。

  转移是显然的。(如果这个都不会,请自定百度“概率dp入门题”)

  然后发现转移有环,还要做决策?

  然后列方程吧。。开心地发现不会解。

  可惜这里是信息学竞赛,不是数学竞赛。由于转移都需要 dp[0][0] 但是开始不知道它,所以考虑二分它,然后和推出来的 dp[0][0] 作比较。

  经过各种瞎猜和乱搞,可以发现一个神奇的事情

  然后就可根据它来确定一次check后,二分的范围。

  另外,由于坑人的精度问题,所以最好不要写while (l + eps < r) ,总之我这么写各种因为精度问题的TLE来了。

Code

 1 /**
 2  * Codeforces
 3  * Problem#866C
 4  * Accepted
 5  * Time: 62ms
 6  * Memory: 4316k
 7  */
 8 #include <bits/stdc++.h>
 9 using namespace std;
10 typedef bool boolean;
11
12 const long double eps = 1e-9;
13 const int binary_lim = 80;
14
15 int n, R;
16 int *fs, *ss;
17 double *ps;
18
19 inline void init() {
20     scanf("%d%d", &n, &R);
21     fs = new int[(n + 1)];
22     ss = new int[(n + 1)];
23     ps = new double[(n + 1)];
24     for(int i = 1; i <= n; i++) {
25         scanf("%d%d", fs + i, ss + i);
26         cin >> ps[i];
27         ps[i] *= 0.01;
28     }
29 }
30
31 boolean vis[51][5105];
32 double f[51][5105];
33
34 double dfs(int d, int t, double &mid) {
35     if(d == n)    return (t > R) ? (mid) : (0);
36     if(vis[d][t])    return f[d][t];
37     vis[d][t] = true;
38     f[d][t] = (dfs(d + 1, t + fs[d + 1], mid) + fs[d + 1]) * ps[d + 1] + (dfs(d + 1, t + ss[d + 1], mid) + ss[d + 1]) * (1 - ps[d + 1]);
39     if(mid < f[d][t])    f[d][t] = mid;
40     return f[d][t];
41 }
42
43 double dp(double mid) {
44     memset(vis, false, sizeof(vis));
45     return dfs(0, 0, mid);
46 }
47
48 inline void solve() {
49     double l = 0, r = 1e9;
50     for(int i = 0; i < binary_lim; i++) {
51         double mid = (l + r) / 2;
52         if(dp(mid) < mid)    r = mid;
53         else    l = mid;
54     }
55     printf("%.9lf", l);
56 }
57
58 int main() {
59     init();
60     solve();
61     return 0;
62 }

原文地址:https://www.cnblogs.com/yyf0309/p/8167220.html

时间: 2024-09-26 20:22:55

Codeforces 866C Gotta Go Fast - 动态规划 - 概率与期望 - 二分答案的相关文章

bzoj 4008 亚瑟王 - 动态规划 - 概率与期望

Description 小 K 不慎被 LL 邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑. 他决定,在脱坑之前,最后再来打一盘亚瑟王.既然是最后一战,就一定要打得漂 亮.众所周知,亚瑟王是一个看脸的游戏,技能的发动都是看概率的.作为一个非 洲人,同时作为一个前 OIer,小 K 自然是希望最大化造成伤害的期望值.但他已 经多年没写过代码,连 Spaly都敲不对了,因此,希望你能帮帮小 K,让他感受一 下当欧洲人是怎样的体验. 本题中我们将考虑游戏的一个简化版模型. 玩家有一套卡牌,共

bzoj 1419 Red is good - 动态规划 - 概率与期望

Description 桌面上有R张红牌和B张黑牌,随机打乱顺序后放在桌面上,开始一张一张地翻牌,翻到红牌得到1美元,黑牌则付出1美元.可以随时停止翻牌,在最优策略下平均能得到多少钱. Input 一行输入两个数R,B,其值在0到5000之间 Output 在最优策略下平均能得到多少钱. Sample Input 5 1 Sample Output 4.166666 HINT 输出答案时,小数点后第六位后的全部去掉,不要四舍五入. (题目太简洁,不需要大意) 这道题和poj的Collecting

Educational Codeforces Round 3:D. Gadgets for dollars and pounds(二分答案+贪心)

看了菊苣的理解才知道怎么写..根本没思路啊一开始... 天数肯定在1~n之间,二分答案. 可以用保存前i天的最低美元和英镑汇率,没有规定哪天买,每天也没有购买数量限制,所以二分出一个答案mid的后,就在前mid天中汇率最低的时候一次性购入最便宜的就行了,judge一下总花费 打印答案的时候以ans为结果,再模拟一遍就好 #include"cstdio" #include"queue" #include"cmath" #include"s

Educational Codeforces Round 80 (Rated for Div. 2)D(二分答案,状压检验)

这题1<<M为255,可以logN二分答案后,N*M扫一遍表把N行数据转化为一个小于等于255的数字,再255^2检验答案(比扫一遍表复杂度低),复杂度约为N*M*logN 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int a[300007][10]; 5 int b[300007][10]; 6 int n,m; 7 int ans,ans2; 8 int t

POJ 3071 Football (动态规划-概率DP)

Football Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2768   Accepted: 1412 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all teams still in the

POJ 2151 Check the difficulty of problems (动态规划-概率DP)

Check the difficulty of problems Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4522   Accepted: 1993 Description Organizing a programming contest is not an easy job. To avoid making the problems too difficult, the organizer usually exp

HDU 4336 Card Collector(动态规划-概率DP)

Card Collector Problem Description In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, for example, if you collect all the 108 people in the famous novel Water Margin, you will win an amazing award. As a

codeforces 235B Let&#39;s Play Osu! 概率dp

题意:给定n表示有n个格子,下面每个格子为O的概率是多少.对于一段连续 x 个O的价值就是 x^2 ;求获得的价值的期望是多少. 思路:n^2=n×(n-1)+n,设ai为第i段连续O的长度,∑ai^2 = ∑[ ai+ ai*(ai-1) ] = ∑ ai*(ai-1) + ∑ai = ∑ C(ai, 2)*2 + ∑ai,那么问题可以转 化为求长度大于1的连续段数*2+O的个数的总期望. ∑ai我们可以理解为O的总个数,所以它的期望为∑pi: C(ai, 2)*2我们可以认 为是连续ai个O

Codeforces 235B Let&#39;s Play Osu! (概率dp求期望+公式变形)

B. Let's Play Osu! time limit per test:2 seconds memory limit per test:256 megabytes You're playing a game called Osu! Here's a simplified version of it. There are n clicks in a game. For each click there are two outcomes: correct or bad. Let us deno