zoukankan      html  css  js  c++  java
  • 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

    https://www.cnblogs.com/heyuhhh/p/11809130.html    (!)

    代码如下:

    typedef long long LL;
    
    const int INF = 0x3f3f3f3f;
    const int maxm = 2e5+5;
    
    LL a[maxm<<2], b[maxm<<2], n;
    
    bool check(int x) {
        int nl = INF, p1 = 1, p2 = 1, now;
        for(int i = 1; i <= 2*n; ++i) {
            p1 = lower_bound(b+1, b+1+4*n, a[i] - x) - b - 1;   
            p2 = upper_bound(b+1, b+1+4*n, a[i] + x) - b - 1;
            nl = min(nl, i-p1);
            now = i - p2 + 1;
            if(now > nl) return false;
        }
        return true;
    }
    
    
    int main() {
        int L;
        scanf("%d%d", &n, &L);
        for(int i = 1; i <= n; ++i)
            scanf("%I64d", &a[i]);
        for(int i = 1; i <= n; ++i)
            scanf("%I64d", &b[i]);
        sort(a+1, a+n+1), sort(b+1, b+n+1);
        for(int i = 1; i <= n; ++i) {
            a[i] += L;
            a[i+n] = a[i] + L;
        }
        for(int i = 1; i <= 3*n; ++i) {
            b[i+n] = b[i] + L;
        }
        int l = 0, r = INF, mid, ans;
        while(l <= r) {
            mid = (l + r) >> 1;
            if(check(mid)) {
                ans = mid;
                r = mid - 1;
            } else 
                l = mid + 1;
        }
        printf("%d
    ", ans);
        return 0;
    }
    View Code

    此处的破环成链,A中接两段,B中接四段,通过图理解比较简单:

    A中每一段对应B中三段,直接对应,从前循环到后,从后循环到前。

  • 相关阅读:
    开发中使用的一些小知识
    vue form表单验证
    团队作业8——第二次项目冲刺(Beta阶段)--第五天
    团队作业8——第二次项目冲刺(Beta阶段)--第四天
    团队作业8——第二次项目冲刺(Beta阶段)--第三天
    团队作业8——第二次项目冲刺(Beta阶段)--第二天
    团队作业8——第二次项目冲刺(Beta阶段)--第一天
    Beta版本冲刺计划及安排
    团队作业7——Alpha冲刺之事后诸葛亮
    团队作业6——展示博客(Alpha版本)
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/11278869.html
Copyright © 2011-2022 走看看