Day3-B-Round Marriage CodeForces-981F

It's marriage season in Ringland!

Ringland has a form of a circle's boundary of length LL. There are nn bridegrooms and nn 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 ii-th bridegroom is located at the distance aiai from the capital in clockwise direction, and the palace of the ii-th bride is located at the distance bibi 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 nn and LL (1n21051≤n≤2⋅105, 1L1091≤L≤109) — the number of bridegrooms and brides and the length of Ringland.

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

The next line contains nn integers b1,b2,,bnb1,b2,…,bn (0bi<L0≤bi<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
2 4
0 1
2 3
Output
1
Input
10 100
3 14 15 92 65 35 89 79 32 38
2 71 82 81 82 84 5 90 45 23
Output
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 11, and the first bride should also walk the same distance. Thus, the inconvenience is equal to 11.

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

 

思路:求最大值最小问题,二分答案查找即可,关键点在于如何快速判断是否可行,这里要用到Hall定理,区间判断是否为完美匹配,将环破成链,距离为min(|A[i]-B[j]|, L-|A[i]-B[j]|),所以B[i]变为B[i],B[i]+L,B[i]-L,构成链,再进行判断A[i]中每个相邻的点是否都相交,如果是则可行,有不相交的说明A中相邻的个数小于A,不满足hall定理,则不可行,假设mid为最大距离,则B可以匹配的区间为[A[i]-mid,A[i]+mid],我们将A集合排序,最优策略就是按照顺序匹配,那么我们可以将可以匹配的B区间的左右端点L、R来判断是否相交,根据最优策略把他们变成[L-i,R-i]判断即可,减i的原因是因为已经有i个A中的点已经匹配(最优策略)。

参考博客:https://blog.csdn.net/c6376315qqso/article/details/82718322

代码如下:

const int maxm = 2e5+10;
const int INF = 0x7fffffff;

int A[maxm], B[maxm << 2], n, L;

bool check(int x) {
    int L = -INF, R = INF;
    for (int i = 0; i < n; ++i) {
        int tl = lower_bound(B + 1, B + 1 + 3 * n, A[i] - x) - B - i;
        int tr = upper_bound(B + 1, B + 1 + 3 * n, A[i] + x) - B - i - 1;
        L = max(L, tl);
        R = min(R, tr);
    }
    return L <= R;
}

int main() {
    scanf("%d%d", &n, &L);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &A[i]);
    }
    for (int i = n; i < 2 * n; ++i) {
        scanf("%d", &B[i]);
    }
    sort(A, A + n), sort(B + n, B + 2 * n);
    for (int i = 0; i < n; ++i)
        B[i] = B[i + n] - L;
    for (int i = 2 * n - 1; i < 3 * n; ++i)
        B[i] = B[i - n] + L;
    int l = 0, r = L - 1, mid;
    while(l <= r) {
        mid = (l + r) >> 1;
        if(check(mid))
            r = mid - 1;
        else
            l = mid + 1;
    }
    printf("%d\n", l);
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/GRedComeT/p/11278869.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值