http://www.elijahqi.win/archives/3549
题目描述
It’s marriage season in Ringland!
Ringland has a form of a circle’s boundary of length
L
L . There are
n
n bridegrooms and
n
n brides, and bridegrooms decided to marry brides.
Of course, each bridegroom should choose exactly one bride, and each bride should be chosen by exactly one bridegroom.
All objects in Ringland are located on the boundary of the circle, including the capital, bridegrooms’ castles and brides’ palaces. The castle of the
i
i -th bridegroom is located at the distance
a_i
ai from the capital in clockwise direction, and the palace of the
i
i -th bride is located at the distance
b_i
bi from the capital in clockwise direction.
Let’s define the inconvenience of a marriage the maximum distance that some bride should walk along the circle from her palace to her bridegroom’s castle in the shortest direction (in clockwise or counter-clockwise direction).
Help the bridegrooms of Ringland to choose brides in such a way that the inconvenience of the marriage is the smallest possible.
输入输出格式
输入格式:
The first line contains two integers
n
n and
L
L (
1 \leq n \leq 2 \cdot 10^{5}
1≤n≤2⋅105 ,
1 \leq L \leq 10^{9}
1≤L≤109 ) — the number of bridegrooms and brides and the length of Ringland.
The next line contains
n
n integers
a_1, a_2, \ldots, a_n
a1,a2,…,an (
0 \leq a_i < L
0≤ai< L ) — the distances from the capital to the castles of bridegrooms in clockwise direction.
The next line contains
n
n integers
b_1, b_2, \ldots, b_n
b1,b2,…,bn (
0 \leq b_i < L
0≤bi< L ) — the distances from the capital to the palaces of brides in clockwise direction.
输出格式:
In the only line print the smallest possible inconvenience of the wedding, where the inconvenience is the largest distance traveled by a bride.
输入输出样例
输入样例#1: 复制
2 4
0 1
2 3
输出样例#1: 复制
1
输入样例#2: 复制
10 100
3 14 15 92 65 35 89 79 32 38
2 71 82 81 82 84 5 90 45 23
输出样例#2: 复制
27
说明
In the first example the first bridegroom should marry the second bride, the second bridegroom should marry the first bride. This way, the second bride should walk the distance of
1
1 , and the first bride should also walk the same distance. Thus, the inconvenience is equal to
1
1 .
In the second example let
p_i
pi be the bride the
i
i -th bridegroom will marry. One of optimal
p
p is the following:
(6,8,1,4,5,10,3,2,7,9)
(6,8,1,4,5,10,3,2,7,9) .
二分距离 然后判断是否能构成完美匹配
那么直接二分答案 然后贪心匹配即可
因为是环 所以我们把 两拨人都复制一下接在后面 bride 复制的时候注意一下
长度小于len的就不添加了 因为将bride从len处切开 这样前面的groom优先和bride匹配
如果bride+len< L 就要再加一遍 因为有可能后面又匹配了前面在
#include<cmath>
#include<cstdio>
#include<cctype>
#include<algorithm>
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0,f=1;char ch=gc();
while(!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
return x*f;
}
const int N=2e5+10;
int a[N],n,L,b[N],gr[N<<1];
inline bool check(int len){
static int br[N<<1];int top=0;
for (int i=1;i<=n;++i) if (b[i]>=len) br[++top]=b[i];
for (int i=1;i<=n;++i) if (b[i]+len<L) br[++top]=b[i]+L;
int i=1,j=1;
while(i<=top){
while(j<=2*n&&abs(br[i]-gr[j])>len) ++j;
if (j==2*n+1) break;++i;++j;
}return i==top+1;
}
int main(){
freopen("cf.in","r",stdin);
n=read();L=read();
for (int i=1;i<=n;++i) a[i]=read();
for (int i=1;i<=n;++i) b[i]=read();
sort(a+1,a+n+1);sort(b+1,b+n+1);
for (int i=1;i<=n;++i) gr[i]=a[i],gr[n+i]=a[i]+L;
int l=0,r=L>>1;
while(l<=r){
int mid=l+r>>1;
if (check(mid)) r=mid-1;else l=mid+1;
}printf("%d\n",l);
return 0;
}