NYOJ 297 GoroSort (数学题)

GoroSort

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 4
描述

Goro has 4 arms. Goro is very strong. You don't mess with Goro. Goro needs to sort an array of N different integers. Algorithms are not Goro's strength; strength is Goro's strength. Goro's plan is to use the fingers on two of his hands to hold down several elements of the array and hit the table with his third and fourth fists as hard as possible. This will make the unsecured elements of the array fly up into the air, get shuffled randomly, and fall back down into the empty array locations.

Goro wants to sort the array as quickly as possible. How many hits will it take Goro to sort the given array, on average, if he acts intelligently when choosing which elements of the array to hold down before each hit of the table? Goro has an infinite number of fingers on the two hands he uses to hold down the array.

More precisely, before each hit, Goro may choose any subset of the elements of the array to freeze in place. He may choose differently depending on the outcomes of previous hits. Each hit permutes the unfrozen elements uniformly at random. Each permutation is equally likely.

输入
The first line of the input gives the number of test cases, T. T test cases follow. Each one will consist of two lines. The first line will give the number N. The second line will list the N elements of the array in their initial order.
1 ≤ T ≤ 100;
The second line of each test case will contain a permutation of the N smallest positive integers.
1 ≤ N ≤ 1000;
输出
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the expected number of hit-the-table operations when following the best hold-down strategy. Answers with an absolute or relative error of at most 10-6 will be considered correct.
样例输入
3
2
2 1
3
1 3 2
4
2 1 4 3
样例输出
Case #1: 2.000000
Case #2: 2.000000
Case #3: 4.000000
提示
In test case #3, one possible strategy is to hold down the two leftmost elements first. Elements 3 and 4 will be free to move. After a table hit, they will land in the correct order [3, 4] with probability 1/2 and in the wrong order [4, 3] with probability 1/2. Therefore, on average it will take 2 hits to arrange them in the correct order. After that, Goro can hold down elements 3 and 4 and hit the table until 1 and 2 land in the correct order, which will take another 2 hits, on average. The total is then 2 + 2 = 4 hits.

注 - 此题为 :NYOJ 297 GoroSort  (数学题)

题意:一串数字,这串数字的顺序是打乱的,但是是1~n之间的不重复的数字,现在有一个人能够将一部分数字按住不动,而用另一只手将余下的这些数字的顺序改变,但是这种改变是没有规则但均等概率出现的重新排列。要求你求出在最坏的情况下至少要改变多少次顺序才能保证这串数字升序排列;
例如  对于:1 3 2,假设摁住1不动,而将3 2的顺序变动的话可能会出现 1 3 2 和1 2 3两种情况。故在最坏的情况下至少需要两次才能使1 3 2按升序排列;

思路:假设只有两个数字不在自己的位置上时,必然会存在将这2个数字位置调换之后就是正确的。而且我们也可以肯定,不满足升序排列的数字都是成对出现的,且在最坏的情况下对2个数字进行随机排列需要两次才能得到正确的排列,所以,其实就是找出不再自己位置上的数的个数即可;

已AC代码:(注意格式)

#include<cstdio>
int main()
{
	int T,n,s,i,a,CASE=1;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		s=0;
		for(i=1;i<=n;++i)
		{
			scanf("%d",&a);
			if(a!=i) // 把不在正确位置上的数放到正确位置上 
				s++;
		}
		printf("Case #%d: %d.000000\n",CASE++,s);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值