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 (1≤n≤2⋅1051≤n≤2⋅105, 1≤L≤1091≤L≤109) — the number of bridegrooms and brides and the length of Ringland.

The next line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai<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 (0≤bi<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+; LL a[maxm<<], b[maxm<<], n; bool check(int x) {
int nl = INF, p1 = , p2 = , now;
for(int i = ; i <= *n; ++i) {
p1 = lower_bound(b+, b++*n, a[i] - x) - b - ;
p2 = upper_bound(b+, b++*n, a[i] + x) - b - ;
nl = min(nl, i-p1);
now = i - p2 + ;
if(now > nl) return false;
}
return true;
} int main() {
int L;
scanf("%d%d", &n, &L);
for(int i = ; i <= n; ++i)
scanf("%I64d", &a[i]);
for(int i = ; i <= n; ++i)
scanf("%I64d", &b[i]);
sort(a+, a+n+), sort(b+, b+n+);
for(int i = ; i <= n; ++i) {
a[i] += L;
a[i+n] = a[i] + L;
}
for(int i = ; i <= *n; ++i) {
b[i+n] = b[i] + L;
}
int l = , r = INF, mid, ans;
while(l <= r) {
mid = (l + r) >> ;
if(check(mid)) {
ans = mid;
r = mid - ;
} else
l = mid + ;
}
printf("%d\n", ans);
return ;
}

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

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

Day3-B-Round Marriage CodeForces-981F的更多相关文章

  1. 【CF981F】Round Marriage(二分答案,二分图匹配,Hall定理)

    [CF981F]Round Marriage(二分答案,二分图匹配,Hall定理) 题面 CF 洛谷 题解 很明显需要二分. 二分之后考虑如果判定是否存在完备匹配,考虑\(Hall\)定理. 那么如果 ...

  2. Codeforces Beta Round #27 (Codeforces format, Div. 2)

    Codeforces Beta Round #27 (Codeforces format, Div. 2) http://codeforces.com/contest/27 A #include< ...

  3. Codeforces 981F. Round Marriage

    Description 一个长度为 \(L\) 的环上有 \(n\) 个黑点和 \(n\) 个白点 , 你需要把黑点和白点配对 , 使得配对点的最大距离最小 , 最小距离定义为两点在环上的两条路径的最 ...

  4. [Educational Round 3][Codeforces 609F. Frogs and mosquitoes]

    这题拖了快一周_(:з」∠)_就把这货单独拿出来溜溜吧~ 本文归属:Educational Codeforces Round 3 题目链接:609F - Frogs and mosquitoes 题目 ...

  5. Codeforces Alpha Round #20 (Codeforces format) C. Dijkstra?(裸的dijkstra)

    题目链接:http://codeforces.com/problemset/problem/20/C 思路:需要用优化过的dijkstra,提供两种写法. #include <iostream& ...

  6. Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861C Did you mean...【字符串枚举,暴力】

    C. Did you mean... time limit per test:1 second memory limit per test:256 megabytes input:standard i ...

  7. Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861B Which floor?【枚举,暴力】

    B. Which floor? time limit per test:1 second memory limit per test:256 megabytes input:standard inpu ...

  8. Codeforces Round #434 (Div. 2, based on Technocup 2018 Elimination Round 1)&&Codeforces 861A k-rounding【暴力】

    A. k-rounding time limit per test:1 second memory limit per test:256 megabytes input:standard input ...

  9. [Educational Round 5][Codeforces 616F. Expensive Strings]

    这题调得我心疲力竭...Educational Round 5就过一段时间再发了_(:з」∠)_ 先后找了三份AC代码对拍,结果有两份都会在某些数据上出点问题...这场的数据有点水啊_(:з」∠)_[ ...

  10. [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]

    这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...

随机推荐

  1. vultr安装kali

    前言 很多国内的主机不支持自定义安装系统,且也不方便下载国外资料:),所以需要使用vultr安装kali. 1.上传镜像 镜像地址填这个(我当时的最新版本) https://cdimage.kali. ...

  2. Java学习资源 - J2SE

    java.lang包教程 Java集合类详解 Java回顾之集合 Java回顾之序列化 Java回顾之反射 深入理解Java:类加载机制及反射 Java 下高效的反射工具包 ReflectASM 使用 ...

  3. 杭电2504 又见GCD

    又见GCD Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  4. Codeforces Round #616 (Div. 2) D

    莫队的模板 struct node{ int l,r,id; }q[maxn]; int cmp(node a,node b) { ) ? a.r < b.r : a.r > b.r); ...

  5. 【PAT甲级】1002 A+B for Polynomials (25 分)

    题意:给出两个多项式,计算两个多项式的和,并以指数从大到小输出多项式的指数个数,指数和系数. AAAAAccepted code: #include<bits/stdc++.h> usin ...

  6. 前端学习 之 CSS(一)

    一:什么是 CSS? ·CSS 指层叠样式表 (Cascading Style Sheets) ·样式定义如何显示 HTML 元素 ·样式通常存储在样式表中 ·把样式添加到 HTML 4.0 中,是为 ...

  7. pikachu-字符型注入(get) #手工注入

    1.检测注入类型 http://127.0.0.1/pikachu-master/vul/sqli/sqli_str.php?name=1&submit=%E6%9F%A5%E8%AF%A2 ...

  8. linux搭建jenkins+github详细步骤

    事情缘由: 现在在做的主要工作是通过jenkins+postman实现api的自动化测试,想要达到的效果是,api自动化测试定时跑脚本的同时,github有新的代码提交,jenkins会自动检测部署新 ...

  9. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 显示代码:按键提示

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. 【原】linux设置网络延迟/丢包操作

    1.tc方式 * 清除设备策略:tc qdisc del root dev eth2 2>/dev/null* 设置设备策略:tc qdisc add dev eth0 root netem l ...