Avito Code Challenge 2018+F. Round Marriage +二分+构造

这里用两倍长度才会出现下标上的m_diff
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)

.

#define happy

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

typedef long long ll;
typedef long double ld;

typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;


#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)

#define all(a) (a).begin(),(a).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second



const ll INF=1e18;
const int MOD=1e9+7;
const int N=1e4+10;

ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int main(){
#ifdef happy
    freopen("in.txt","r",stdin);
#endif
    ll n=rd(),L=rd();
    vector<ll> a(n),b(n);
    rep(i,0,n-1)a[i]=rd();
    rep(i,0,n-1)b[i]=rd();
    sort(all(a));sort(all(b));
    rep(rot,0,4){
        rep(i,0,n-1)
        a.pb(a[i]+L*(rot+1));
        rep(i,0,n-1)
        b.pb(b[i]+L*(rot+1));
    }
    ll low=0,high=L-1;
    while(low<high){
        ll mid=(low+high)>>1;
        int j1=0,j2=0;
        int found=0;
        int m_diff=(int)-1e9;
        rep(i,2*n,4*n-1){
            while(b[j1]<a[i]-mid)j1++;
            while(b[j2]<=a[i]+mid)j2++;
            m_diff=max(m_diff,j1-i);
            if(j2-i-1<m_diff){
                found=1;
                break;
            }
        }
        if(found)low=mid+1;
        else high=mid;
    }
    printf("%d\n",low);
}

猜你喜欢

转载自blog.csdn.net/ujn20161222/article/details/80488557