CodeForces 981F Round Marriage(二分答案+贪心)

F. Round Marriage
time limit per test:3 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

It's marriage season in Ringland!

Ringland has a form of a circle's boundary of length L. There are n bridegrooms and 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-th bridegroom is located at the distance ai from the capital in clockwise direction, and the palace of the i-th bride is located at the distance 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.

Input

The first line contains two integers n and L (1n2105, 1L109) — the number of bridegrooms and brides and the length of Ringland.

The next line contains n integers a1,a2,,an (0ai<L) — the distances from the capital to the castles of bridegrooms in clockwise direction.

The next line contains n integers b1,b2,,bn (0bi<L) — the distances from the capital to the palaces of brides in clockwise direction.

Output

In the only line print the smallest possible inconvenience of the wedding, where the inconvenience is the largest distance traveled by a bride.

Examples
Input
Copy
2 4
0 1
2 3
Output
Copy
1
Input
Copy
10 100
3 14 15 92 65 35 89 79 32 38
2 71 82 81 82 84 5 90 45 23
Output
Copy
27
Note

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, and the first bride should also walk the same distance. Thus, the inconvenience is equal to 1.

In the second example let pi be the bride the i-th bridegroom will marry. One of optimal p is the following: (6,8,1,4,5,10,3,2,7,9)



        大致题意:给你n个新郎和新娘以及他们所在的位置,现在要两两配对,配对有个代价,就是二者的距离。现在问,这n对新人中,最大代价的最小值是多少。

        这体显然是一个二分答案,二分最大代价最小值,再判断是否可行。那么问题的 关键就是如何判断问题是否可行。数据范围是2e5,所以要求我们要在O(N)的复杂度内完成这个判断。对于每一对人i和j,他们的代价是min(|a[i]-b[j]|,L-|a[i]-b[j]|),分细一点可以把情况分为两种。当a[i]>b[j]时,代价为min(a[i]-b[j],b[j]+L-a[i]);当a[i]<=b[j]时,代价为min(b[j]-a[i],a[i]-(b[j]-L))。于是,我可以把出现的数字分成4种:a[i]、b[j]、b[j]+L和b[j]-L,分别对应这几种情况。把b数组拓展成3*n,如此判断的时候就只需要判断a[i]-b[j]和b[j]-a[i]了。再分别对两个数组排序,每次依次处理每一个a[i],逐步缩小可选范围。

        为了使得最大代价最小,所以得考虑贪心策略。显然每一个a[i]要在满足条件的情况下,尽量选择远的,把近的留给后面的。每次根据枚举的最大值x缩小范围[l,r],当a[i]-b[l]>x时,l++;当b[r]-a[i]>x时r--。缩小范围之后,当前的a[i]要么选择b[l]要么选择b[r],对应l++和r++。最后,我们再判断以下这个可行范围是否符合要求l<=r,如果符合那么这个x可行,否则不可行。

        另外,有可能新郎和新娘在同一个地方,因此最大代价最小值的枚举区间要包括0。具体见代码:

#include <bits/stdc++.h>
#define N 200010
using namespace std;

int n,m,a[N],b[N<<2];

bool check(int x)
{
    int l=1,r=3*n;
    for(int i=1;i<=n;i++)
    {
        while(a[i]-b[l]>x) l++;
        while(b[r]-a[i]>x) r--;
        l++; r++;
    }
    return l<=r;
}

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=n;i++)
    {
        cin>>b[i];
        b[n+i]=b[i]+m;
        b[2*n+i]=b[i]-m;
    }
    sort(a+1,a+1+n);
    sort(b+1,b+1+3*n);
    int l=0,r=(m+1)/2,mid,ans;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if (check(mid)) ans=mid,r=mid-1;
                           else l=mid+1;
    }
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u013534123/article/details/80519130