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;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

时间: 2024-08-08 01:27:24

UVA - 11637 Garbage Remembering Exam (组合+可能性)的相关文章

UVA 11637 - Garbage Remembering Exam(组合概率)

UVA 11637 - Garbage Remembering Exam 题目链接 题意:大概意思是,有n个单词,分别打乱放在一个环形的,一个非环形里面,环形的两个单词距离为顺时针逆时针的最小值,非环形的就是位置的差的绝对值,如果有一对单词,在两个里面的距离都是不大于k,那么这单词为无效单词,问平均会出现多少个无效单词 思路:组合概率,假设在非环形形成了一个随机序列,那么我们给它标号1-n,如果我们能分别算出1-n的有效概率,那么就等于算出了无效概率,那么有效概率等于和它距离大于k的那些位置的所

uva 11637 - Garbage Remembering Exam(概率)

题目链接:uva 11637 - Garbage Remembering Exam 题目大意:大白数里有很详细的题意. 解题思路:对于有序的序列来说,考虑每个位置为有效性的概率.C(2?kn?1?x)?A(2k2k)?A(n?1?xn?1?x)A(n?1n?1) x为考虑当前位置,然后与该位置距离小于等于k的位置个数.该位置有效的话,则对应的要将原先邻近的2*k个单词放到另外的位置上. #include <cstdio> #include <cstring> #include &l

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 troub

uva 11927 - Games Are Important(组合游戏+记忆化)

题目链接:uva 11927 - Games Are Important 题目大意:给出一张无环有向图,并给出每个节点上的石子数,每次操作可以选择一个石子,向下一个节点移动.两人轮流操作,直到不能操作为失败者. 解题思路:有了图之后,用记忆化的方式处理出每个节点的SG值,取所有石子数为奇数的节点的Nim和. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; con

UVA 10755 Garbage Heap 最大子立方体和

点击打开链接 10755 - Garbage Heap Time limit: 3.000 seconds 最大子立方体和比最大子矩阵多一维,同样转换为一维,然后求最值. #include<stdio.h> #include<string.h> #include<algorithm> #define ll long long #define inf 1ll<<60//加ll using namespace std; ll sum[27][27][27]; l

UVa 10755 Garbage Heap (暴力+前缀和)

题意:有个长方体由A*B*C组成,每个废料都有一个价值,要选一个子长方体,使得价值最大. 析:我们暴力枚举上下左右边界,然后用前缀和来快速得到另一个,然后就能得到长方体,每次维护一个最小值,然后差就是最大值. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #incl

UVA - 10755 Garbage Heap

题目大意:给出一个三维矩阵,求子矩阵和最大值. 解题思路:现将各个平面上的矩阵记录下矩阵和,然后可以枚举二维上的矩阵,映射成三维去做. #include <cstdio> #include <algorithm> using namespace std; int main() { int T; scanf("%d", &T); while (T--) { int A, B, C; long long tmp, s[25][25][25] = {0}; s

数学&#183;概率

相关习题: Uva 11637 Garbage Remembering Exam 代码: 1 #include <algorithm> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <queue> 6 #include <map> 7 #include <set> 8 #include <ctime> 9 #inc

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y