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 (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
2 4
0 1
2 3
1
10 100
3 14 15 92 65 35 89 79 32 38
2 71 82 81 82 84 5 90 45 23
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的更多相关文章
- 【CF981F】Round Marriage(二分答案,二分图匹配,Hall定理)
[CF981F]Round Marriage(二分答案,二分图匹配,Hall定理) 题面 CF 洛谷 题解 很明显需要二分. 二分之后考虑如果判定是否存在完备匹配,考虑\(Hall\)定理. 那么如果 ...
- Codeforces Beta Round #27 (Codeforces format, Div. 2)
Codeforces Beta Round #27 (Codeforces format, Div. 2) http://codeforces.com/contest/27 A #include< ...
- Codeforces 981F. Round Marriage
Description 一个长度为 \(L\) 的环上有 \(n\) 个黑点和 \(n\) 个白点 , 你需要把黑点和白点配对 , 使得配对点的最大距离最小 , 最小距离定义为两点在环上的两条路径的最 ...
- [Educational Round 3][Codeforces 609F. Frogs and mosquitoes]
这题拖了快一周_(:з」∠)_就把这货单独拿出来溜溜吧~ 本文归属:Educational Codeforces Round 3 题目链接:609F - Frogs and mosquitoes 题目 ...
- Codeforces Alpha Round #20 (Codeforces format) C. Dijkstra?(裸的dijkstra)
题目链接:http://codeforces.com/problemset/problem/20/C 思路:需要用优化过的dijkstra,提供两种写法. #include <iostream& ...
- 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 ...
- 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 ...
- 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 ...
- [Educational Round 5][Codeforces 616F. Expensive Strings]
这题调得我心疲力竭...Educational Round 5就过一段时间再发了_(:з」∠)_ 先后找了三份AC代码对拍,结果有两份都会在某些数据上出点问题...这场的数据有点水啊_(:з」∠)_[ ...
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
随机推荐
- Bugku - CTF加密篇之滴答~滴
滴答~滴 答案格式KEY{xxxxxxxxx}
- 【安全运维】Vim的基本操作
i 插入模式 : 末行模式 a 光标后插入 A 切换行末 I 切换行首 o 换行 O 上一行 p 粘贴 u 撤销 yy 复制 4yy 复制四行 dd (剪切)删除一行 2dd (剪切)删除两行 D 剪 ...
- Linux终端的一些快捷键命令
一.初识linux的终端种类:本地.远程 查看本终端命令: #tty 命令,看到当前所处的终端 #(w)who 命令,看到系统中所有登录的用户 其中,tty 终端为表示在本地命令行模式下打开的终端:p ...
- 阅读build to win的个人感想
一个程序员要向各个方面学习,向市场.向用户学习等,不能局限于一方面.除此以外还要有自己的想法,要懂得创新,也需要在各个方面都有所突破,有所超越,实力才是取得胜利的根关键.
- C++11常用特性介绍——Lambda表达式
一.C++11采用配对的方括号[]来创建一个匿名函数并执行,如: #include <iostream> int main() { auto func = []{ std::cout &l ...
- Tensorflow机器学习入门——ModuleNotFoundError: No module named 'tensorflow.keras'
这个bug的解决办法: # from tensorflow.keras import datasets, layers, models from tensorflow.python.keras imp ...
- SpringBoot与Mybatis-plus整合,代码生成mvc层
一.添加pom依赖 <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifac ...
- Tomcat认识
Tomcat目录结构: bin:存放启动和关闭的一些脚本 common:共享(部署在该服务器上的一些)jar包 conf:存放服务器的一些配置文件 webapps:部署文件 work:服务器运行时,产 ...
- idea中scala语言自动补全变量的同时,也自动补全类型
IDE是IDEA,scala中,在new一个对象时,通过快捷键ctrl + Alt + V自动补全变量,但是我还想自动补全变量的类型,就像图中所示,在Specify type前面自动帮你打勾. 可以按 ...
- js中for循环(原生js)
1,普通for循环,经常用的数组遍历 var arr = [1,2,3,4,5]; for ( var i = 0; i <arr.length; i++){ console.log(arr[i ...