Segment
Accepts: 17
Submissions: 47
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 131072/131072 K (Java/Others)
对于一种可行的排列(使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;
}