赛码"BestCoder"杯中国大学生程序设计冠军赛hdu5216


Segment

Accepts: 17
Submissions: 47
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 131072/131072 K (Java/Others)
Problem Description

BrotherK has a white wood stick of length N which consists of N linked sticks of length 1. We number these N sticks from number 1 to N , from left to right.

Today, BrotherK is bored, so he wants to cut his wood stick.

First, he chooses two arrays A, B : each one has M elements : A1 ~ AM , B1 ~ BM . Next he chooses a permution of 1 ~ M randomly(All possible permutations have equal probability). Assume the permutaion is P1 ~ PM . If there exists an I in [1, M] such that AI > BPI , BrotherK will choose another permutation randomly, until there is not any I such that AI > BPI .

For each I in [1, M] , he will paint black on sticks between the interval [AI, BPI] .

Finally, BrotherK cuts all black sticks. There may remain some white sticks.

Now, BrotherK wants to know the expected length of the longest white stick.(If after painting, all sticks are black, then we consider the length as 0).

Input

The first line contains a single integer T , indicating the number of test cases.

Each test case begins with two integers N, M , indicating the length of the original stick, and the number of the elements in array A, B .

The second line contains M numbers, from A1 to AM .

The third line contains M numbers, from B1 to BM .

T is about 20.

1  N  1000000 .

1  M  50 .

1  Ai,Bi  N.

Output

For each test, print one lines.

If BrotherK can't find a valid permutation, print "Stupid BrotherK!" (without quotation marks).

Otherwise, print the expected length. (correct to six decimal places)

Sample Input
2
5 2
1 3
2 4
5 1
1
5
Sample Output
1.000000
0.000000


   
   
Hint
In the first case, BrotherK will stop with permutation {1, 2}.
官方题解:

对于一种可行的排列(使BrotherK停下来的排列)P:
若存在i,j, 使得:
Ai < Aj, BPi > BPj
那么即使我们交换Pi和Pj的值,被涂黑的区间仍然是一样的
因此我们可以不断进行这种交换,使得不存在满足
Ai < Aj, BPi > BPj
的i,j,并且这种交换始终不影响最终答案
到此可以发现,所有合法方案的答案都是一样的,并不需要计算概率
因此只需将A, B分别排序,若排序后存在Ai>Bi则说明没有合法的排列
否则直接计算出答案,并输出六位小数即可
时间复杂度O(MlogM)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100010;
int N,M;
int A[maxn],B[maxn];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&N,&M);
		for(int i=1;i<=M;i++)scanf("%d",&A[i]);
		for(int i=1;i<=M;i++)scanf("%d",&B[i]);
		sort(A+1,A+1+M);
		sort(B+1,B+1+M);
		bool flag=true;
		for(int i=1;i<=M;i++)
			if(A[i]>B[i]){flag=false;break;}
		if(!flag){printf("Stupid BrotherK!\n");}
		else
		{
			int ans=max(A[1]-1,N-B[M]);
			int last=0;
			for(int i=1;i<=M;i++)
			{
				if(A[i]>last)
				{
					ans=max(ans,A[i]-last-1);
				}
				last=B[i];
			}
			printf("%.6lf\n",double(ans));
		}
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值