1. 程式人生 > >CodeForces 981F Round Marriage(二分答案+貪心)

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

F. Round Marriagetime limit per test:3 secondsmemory limit per test:256 megabytesinput:standard inputoutput: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.

ExamplesInputCopy
2 4
0 1
2 3
OutputCopy
1
InputCopy
10 100
3 14 15 92 65 35 89 79 32 38
2 71 82 81 82 84 5 90 45 23
OutputCopy
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;
}