LightOJ - 1274 Beating the Dataset —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1274
| Time Limit: 4 second(s) | Memory Limit: 32 MB |
You are in a contest, and unfortunately you don't have much time. You have one problem in hand; you just glanced at the sample output and found that it just wants 'YES' or 'NO'. So, you have made another plan instead of solving the problem as you know the system very well.
For this problem, every test case is stored in a separate file. When a submission is found, the system successively runs the solution on all tests of a problem, and for each test the checking process goes as follows. The input is copied to the file input.txt. Then the solution is launched. It reads the input from the file input.txt and writes the result to the file output.txt. When it finishes, the correct answer is copied to the file answer.txt. If the contents of the files answer.txt and output.txt match, the test is assumed to be passed; otherwise, the test is not passed.
So, you decided to write a program that would operate as follows. If the folder containing the program doesn't contain the file answer.txt (i.e. the program is run on the first test), then the program outputs "YES". Otherwise, the program outputs the contents of the file answer.txt. And before the contest, the sizes of the data files are given to you.
And it's clear that the size of the file with the answer "YES" is 3 bytes, the size of the file with the answer "NO" is 2 bytes, and all the variants of the order of tests are equally probable. Now you want to calculate the average number of tests that your solution won't pass.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with a line containing two integers n (1 ≤ n ≤ 5000) and s (2n ≤ s ≤ 3n) where n denotes the number of data sets and s denotes the total size of the answer files.
Output
For each case, print the case number and the average number of tests your solution won't pass. Error less than 10-6 will be ignored.
Sample Input |
Output for Sample Input |
|
4 3 7 1 2 1 3 4 10 |
Case 1: 2 Case 2: 1 Case 3: 0 Case 4: 2.5000000000 |
Note
For the first case, one of the three answers is "YES" and two answers are "NO". If the order of tests is "YES-NO-NO", then your solution won't pass the second test only; if the order is "NO-YES-NO", then it will pass none of the tests; if the order is "NO-NO-YES", the solution won't pass the first and the third tests.
题意:
是一个人做ACM题,这道题的数据的字节数和提问数已经给出,答案只有YES或NO。那个人做不来这道题,只能蒙,一开始一定蒙YES,数据会告诉你这题的正确答案,之后下一题用上一题的正确答案蒙,求蒙错的数据组数的期望数量。
题解:
1.可知:
yes + no = n
3*yes + 2*no = s
因此可以联立解出yes、no的个数。
2.dp[i][j][isYes]:处理到第i个位置,前面已经有j个yes,并且第i个是yes\no时(0代表yes)的情况下,后面错误次数的期望值。
3.可知第i+1个位置出现yes的概率为:py = (yes-j)/(n-i),no的概率为:pw = (no-(i-j))/(n-i)。
3.1 当j+1<=yes时,即还可以放yes,那么:
dp[i][j][0] = py*dp[i+1][j+1][0] + pw*(dp[i+1][j][1]+1);
dp[i][j][1] = py*(dp[i+1][j+1][0]+1) + pw*dp[i+1][j][1];
3.2 当j==yes时,即yes已经放完了,那么后面只能放no:
dp[i][j][0] = pw*(dp[i+1][j][1]+1);
dp[i][j][1] = pw*dp[i+1][j][1];
4.由于数组太大,所以要用滚动数组。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 5e3+; double dp[][MAXN][];
int main()
{
int T, kase = ;
scanf("%d", &T);
while(T--)
{
int n, s;
scanf("%d%d", &n, &s);
int yes = s - *n;
int no = *n - s;
dp[n%][yes][] = dp[n%][yes][] = ;
for(int i = n-; i>=; i--)
{
int now = i%, nex = (i+)%;
int minYes = max(,i-no), maxYes = min(i,yes);
for(int j = minYes; j<=maxYes; j++)
{
double py = 1.0*(yes-j)/(n-i);
double pw = 1.0*(no-(i-j))/(n-i);
if(j+<=yes)
{
dp[now][j][] = py*dp[nex][j+][] + pw*(dp[nex][j][]+);
dp[now][j][] = py*(dp[nex][j+][]+) + pw*dp[nex][j][];
}
else
{
dp[now][j][] = pw*(dp[nex][j][]+);
dp[now][j][] = pw*dp[nex][j][];
}
}
}
printf("Case %d: %.10lf\n", ++kase, dp[][][]);
}
}
LightOJ - 1274 Beating the Dataset —— 期望的更多相关文章
- 【非原创】LightOJ-1274 Beating the Dataset【期望dp】
学习博客:戳这里
- LightOJ 1030 Discovering Gold(期望)
Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell o ...
- LightOj:1030-Discovering Gold(期望dp模板)
传送门:http://www.lightoj.com/volume_showproblem.php?problem=1030 Discovering Gold Time Limit: 2 second ...
- LightOJ 1030 Discovering Gold (概率/期望DP)
题目链接:LightOJ - 1030 Description You are in a cave, a long cave! The cave can be represented by a \(1 ...
- LightOJ 1287 Where to Run(期望)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1287 题意:给定一个n个点的无向图(0到n-1),你开始在0.你开始遍历这个图 ...
- LightOj_1274 Beating the Dataset
题目链接 题意: 给一个文档, 这个文档由yes .no 组成, 共有s个byte, 共有n个yes.no. 假设yes的个数为yes_num, no的个数为no_num. 将这n个数进行排列, 对于 ...
- LightOJ 1030 Discovering Gold (期望)
https://vjudge.net/problem/LightOJ-1030 题意: 在一个1×N的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得到该格子的金币. 现在从1格子开始,每次 ...
- LightOJ - 1287 Where to Run —— 期望、状压DP
题目链接:https://vjudge.net/problem/LightOJ-1287 1287 - Where to Run PDF (English) Statistics Forum T ...
- LightOJ - 1027 A Dangerous Maze —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1027 1027 - A Dangerous Maze PDF (English) Statistics For ...
随机推荐
- Linux学习之十六-Linux用户管理
Linux用户管理 Linux系统跟Windows系统一样,可以创建不同的用户,不同的用户组.在不同用户下使用系统具有相应的权限 创建一个普通用户时,会修改几个文件,拷贝一些初始文件到用户家目录中 修 ...
- suid sgid sbit chattr lsattr find
suid 一般用于二进制可执行文件不可用于shell脚本和目录,suid代表当用户执行此二进制文件时,暂时具有此文件所有者的权限 chmod 4xxx binfile sgid 一般用于目录,sgid ...
- SVN切分支步骤
1.右键project选择Brankch/Tag 2.选择SVN路径并在改路径下填写project名称 3.选择最新版本号 4.填写必要的凝视备忘,方便日后查看 5.刷新父文件夹文件夹.下载被切出来的 ...
- 高速掌握Lua 5.3 —— Lua与C之间的交互概览
Q:什么是Lua的虚拟栈? A:C与Lua之间通信关键内容在于一个虚拟的栈.差点儿全部的调用都是对栈上的值进行操作,全部C与Lua之间的数据交换也都通过这个栈来完毕.另外,你也能够使用栈来保存暂时变量 ...
- C#比較对象的相等性
对于相等的机制全部不同,这取决于比較的是引用类型还是值类型.以下分别介绍引用类型和值类型的相等性. 1.比較引用类型的相等性 System.Object定义了三种不同的方法,来比較对象的相等性:Ref ...
- Buck电路匹配和二极管仿真模式
Buck带同步整流,关闭二极管仿真模式会使空载损耗大 利用二极管仿真模式提高降压转换器轻负载效率 Buck电路工作原理以及三种工作模式分析 一.Buck电路原理图 Buck电路,又称降压电路,其基 ...
- webStorm 多列编辑
webStorm可以像Sublime一样使用列编辑,只是区别在于webStorm只可以编辑连续列表. 按住alt键鼠标选择一列,然后输入文字就会编辑多行,这个功能很赞,比较实用(按住ALT键选中之后, ...
- 还需要学习的十二种CSS选择器
在前面的文章中,我们在介绍了<五种你必须彻底了解的CSS选择器>,现在向大家介绍,还需要学习的另外十二种CSS选择器.如果你还没有用过,就好好学习一下,如果你已经熟知了就当是温习. 一.X ...
- servletResponse 控制浏览器缓存
//当访问一些资源文件时,我们希望,访问一次后,资源文件能够在缓存在浏览器中,当我们再次访问该资源时 //直接从缓存中去取,这样可以减少服务器的压力 package response; import ...
- 三、Silverlight中使用MVVM(三)——进阶
这篇主要引申出Command结合MVVM模式在应用程序中的使用 我们要做出的效果是这样的 就是提供了一个简单的查询功能将结果绑定到DataGrid中,在前面的基础上,这个部分相对比较容易实现了 我们在 ...