UVA - 11637 Garbage Remembering Exam (组合+可能性)
Little Tim is now a graduate,and is thinking about higher studies. However, he first needs to appear in anexam whose preparation alone includes memorizing the meanings of over 3500words!
After going through the list afew times, however, he sees trouble – he can remember all the word-meanings aslong as they keep coming in the same order. When quizzed in random order,however, he is at a complete loss. So, as any decent programmer would, hewrites
a random shuffler to help him quiz himself.
To his dismay, however, hefinds that words that were near each other in the original list quite often endup being close together in the shuffled list as well. He does not like this atall, and suspects that his random shuffler may have some kind of a bug.
Butbefore he recodes his shuffler he wants your help to make sure that theshuffler is indeed faulty.
So he is asking for your helpin determining how likely such events are even if the list is indeed gettingcompletely randomly shuffled, and even if his program is working perfectly.
Given the size of the list N,and a proximity factor K, you are to determine the expected number of
wastedwords in a shuffled list assuming that all possible shufflings are equallylikely. Informally, two words are considered
wasted if they appear at adistance less than or equal to K in both the lists. Assume that theoriginal list is
cyclical and shuffled list is linear (non-cyclical).
Formally, let us suppose thattwo words A and B have indices oa and
ob inthe original list and indices sa and
sb inthe shuffled list, respectively (all indices are 0-based). Then both the wordsare considered
wasted if:
and
Input
The input consists of a seriesof cases. Each case consists of two integers
N and K on a singleline. You may assume that 1≤K≤N≤100000.Input is terminated by a line containing two 0s, and has at most 125 lines ofinput.
Output
Output oneline for each test case except the last one. Each line of output should be ofthe form “Case X: Y”, where X is the serial number of output and Y is the expected number of wasted words in theshuffled list, printed with exactly four digits after
the decimal point, withrounding if necessary.
SampleInput Outputfor Sample Input
5 2 5 1 0 0 |
Case 1: 5.0000 Case 2: 3.5000 |
题意:输入n和k,你的任务是计算平均情况下。无效单词的个数,计算方法是:两个单词在又一次排列后的位置不超过k
思路:我们先计算有效的位置。枚举后。从剩下的选出2*k来计算,用log来计算
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 100005; int n, k;
long double f[maxn]; void solve() {
if (n == 1) {
printf("0.0000\n");
return;
}
if (n <= 2 * k + 1) {
printf("%d.0000\n", n);
return;
} int N = k << 1, p;
long double sum = 0;
for (int i = 1; i <= n; i++) {
p = max(i-1-k, 0) + max(n-k-i, 0);
if (p < N)
continue;
sum += exp(f[p] + f[n - N - 1] - f[n - 1] - f[p - N]);
}
printf("%.4lf\n", (double)(n - sum));
} int main() {
f[0] = f[1] = 0;
for (int i = 2; i <= maxn; i++)
f[i] = f[i-1] + log((long double) i); int cas = 1;
while (scanf("%d%d", &n, &k) != EOF && n) {
printf("Case %d: ", cas++);
solve();
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
UVA - 11637 Garbage Remembering Exam (组合+可能性)的更多相关文章
- UVA 11637 Garbage Remembering Exam
#include <iostream> #include <stdio.h> #include <cstring> #include <math.h> ...
- uva 11673 Garbage Remembering Exam (概率)
题目链接: http://vjudge.net/problem/viewProblem.action?id=42000 该过程为随即过程,因此总期望值等于个单词对应的期望值,即它们wasted的概率 ...
- 🔺 Garbage Remembering Exam UVA - 11637()
题目大意:给你N个单词,有两种方法随机排列,一种随机排成一行,另一种随机排成一圈,当两个单词之间的距离在两种排列中都严格小于K时,则这两个单词构成无效单词,问无效单词的期望. 解题思路:首先对于一排单 ...
- uva 10755 - Garbage Heap
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 12906 Maximum Score 排列组合
Maximum Score Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/vie ...
- UVa 10755 Garbage Heap (暴力+前缀和)
题意:有个长方体由A*B*C组成,每个废料都有一个价值,要选一个子长方体,使得价值最大. 析:我们暴力枚举上下左右边界,然后用前缀和来快速得到另一个,然后就能得到长方体,每次维护一个最小值,然后差就是 ...
- UVa 11538 Chess Queen (排列组合计数)
题意:给定一个n*m的棋盘,那么问你放两个皇后相互攻击的方式有多少种. 析:皇后攻击,肯定是行,列和对角线,那么我们可以分别来求,行和列其实都差不多,n*A(m, 2) + m*A(n, 2), 这是 ...
- .Uva&LA部分题目代码
1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...
- LeetCode 216. Combination Sum III (组合的和之三)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
随机推荐
- poj2253(最短路小变形)
题目连接:http://poj.org/problem?id=2253 题意:给出一个无向图,求一条1~2的路径使得路径上的最大边权最小. 分析:dij将距离更新改成取最大值即可,即dp[i]表示到达 ...
- Spring3.0官网文档学习笔记(七)--3.4.2
3.4.2 依赖与配置的细节 3.4.2.1 Straight values (primitives, Strings, and so on) JavaBeans PropertyE ...
- 服务器编程入门(3)TCP协议详解
问题聚焦: 本节从如下四个方面讨论TCP协议: TCP头部信息:指定通信的源端端口号.目的端端口号.管理TCP连接,控制两个方向的数据流 TCP状态转移过程:TCP连接的任意一 ...
- ORACLE 实验二
实验二:数据操纵 实验学时:4学时 实验类型:综合型 实验要求:必修 一.实验目的 1.掌握SQL数据查询语句: 2.掌握SQL聚集函数的使用. 3.掌握SQL插入.改动.删除语句的使用. 二.实验内 ...
- Swift 的类、结构体、枚举等的构造过程Initialization(下)
类的继承和构造过程 类里面的全部存储型属性--包含全部继承自父类的属性--都必须在构造过程中设置初始值. Swift 提供了两种类型的类构造器来确保全部类实例中存储型属性都能获得初始值,它们各自是指定 ...
- SWT的CheckBoxTreeView的上级菜单与下级菜单的选中的实现
是不是很神奇? treeViewer.addCheckStateListener(new ICheckStateListener() { @Override public void checkStat ...
- Cacti监控Tomcatserver实现过程
1 首先去官网上面下载通用的监控模板 一般使用TomcatStats-0.1.zip 模板居多,下载地址: http://forums.cacti.net/download/file.php?id=1 ...
- Hibernate常用Annotation标签说明
@ javax.persistence.Entity 实体类定义,该标签表示当前类是一个Hibernate的数据库实体,对应着数据库中的某个表 位置:用于类级别 参数:无 样例:@Entity 注意: ...
- 关于SSIS批量抽取Excel文件报0x80004005错误的解决办法
原文:关于SSIS批量抽取Excel文件报0x80004005错误的解决办法 标题: Microsoft Visual Studio ------------------------------ Pa ...
- WPF换肤之三:WPF中的WndProc
原文:WPF换肤之三:WPF中的WndProc 在上篇文章中,我有提到过WndProc中可以处理所有经过窗体的事件,但是没有具体的来说怎么可以处理的. 其实,在WPF中,要想利用WndProc来处理所 ...