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. java 事务解释。

    面试的时候,面试人员总喜欢问在spring中, 1. 如果一个主线程上有一个事务,在事务中开启了一个线程.子线程跑出异常,对主线程有没有影响,或者主线程产生异常对子线程有没有影响. 这个时候,你只要记 ...

  2. Docker 安装 Filebeat

    使用同版本镜像 7.4.1 1.下载Filebeat镜像 docker pull store/elastic/filebeat: docker images 2.下载默认官方配置文件wget http ...

  3. ES-基本操作

    (1)创建索引 put 192.168.247.197:9200/type2 /type2 (2)创建映射 post 192.168.247.197:9200/type2/type/_mapping ...

  4. inode节点使用率过大处理

    当发现某个分区下的inode使用率过大时,需要找到该分区下的某些目录里有哪些文件可以清理. 查找某个目录下一个月或两个月之前的文件,然后删除# find . -type f -mtime +30 |w ...

  5. 【网摘】JS 或 jQuery 获取当前页面的 URL 信息

    1.设置或获取对象指定的文件名或路径. window.location.pathname 2.设置或获取整个 URL 为字符串. window.location.href 3.设置或获取与 URL 关 ...

  6. Docker for windows修改默认镜像文件位置

    docker版本为18.06 windows上安装的docker其实本质上还是借助与windows平台的hyper-v技术来创建一个Linux虚拟机,你执行的所有命令其实都是在这个虚拟机里执行的,所以 ...

  7. 设计模式01 创建型模式 - 建造者模式(Build Pattern)

    参考 1. Builder Design Pattern | Youtube 2. 建造者模式(Builder和Director)| 博客园 3. 深入理解Builder模式 | 简书 建造者模式(B ...

  8. gitlab的搭建与使用(一)

    yum install curl policycoreutils openssh-server openssh-clients postfix -y systemctl enable sshd sys ...

  9. IELTS Writing Task 2: 'music' essay

    IELTS Writing Task 2: 'music' essay Here's my band 9 sample answer for the question below. Some peop ...

  10. HTML table表头排序箭头绘制法【不用箭头图片】

    效果图 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="ut ...