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 题目大 ...
随机推荐
- Jmeter进行分布式性能测试
由于Jmeter本身的瓶颈,当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发用户就有些力不从心,甚至还会引起JAVA内存溢出的错误.要解决这个问题,可以使用分布式测试,运行多台机器运行所谓的 ...
- twisted task.cpperator
twisted task.cpperator 1. twisted task.cpperator 1.1. 简介-cooperator 官方文档: https://twistedmat ...
- 【JS 常用操作】全选、给后来元素增加事件
11 //全选 $("#allCheckbox").click(function () { var checkedStatus = this.checked; //alert(ch ...
- MySQL自动备份实战--xtrabackup备份
MySQL数据备份企业实战.制作shell脚本 功能1:使用xtrabackup以每周为一个备份周期做备份(数据库+二进制日志,备份至本地/data/backup).提示: 周一某个时间点做一次完全备 ...
- SVN安装不成功,提示Invalid driver H:
本来我的SVN安装在H盘,后来我把包含H盘的硬盘下下来了,这样H盘就不存在了. 这时候我想重新安装SVN,点击安装包,结果提示Invalid driver H,怎么都不能安装成功. 这时候我去注册表里 ...
- Java Web 前端资源文件的路径问题
WEB-INF是Java Web应用的安全目录,在部署时用于存放class文件.项目用到的库(jar包).Java Web应用的配置文件web.xml. 浏览器不能访问此目录下的资源,比如在WEB-I ...
- pikachu-字符型注入(get) #手工注入
1.检测注入类型 http://127.0.0.1/pikachu-master/vul/sqli/sqli_str.php?name=1&submit=%E6%9F%A5%E8%AF%A2 ...
- 投资人分享答疑----HHR计划----以太直播课第三课
分享大纲:(祥峰投资) 一,投资人会看什么: 1,赛道定位:“生意”还是“独角兽-to be”? 2,如何退出?上市还是收购? 3, 团队能力,愿景力 4,壁垒:数据和价值 5,价格 二,融资需要准 ...
- 2 CSS盒子模型&边框&轮廓&外边距&填充&分组嵌套&尺寸&display与visibility
盒子模型 Box Model 所有HTML元素可以看做盒子,CSS模型本质上是一个盒子,封装周围的HTML元素 包括:边距,边框,填充和实际内容 盒子模型允许我们在其他元素和周围元素边框之间的空间放 ...
- C#多态学习总结
面向对象编程三大特点 封装 继承 多态.今天我把自己学习多态的过程进行总结 多态 就是 同一个方法在不同情况下,会表选不同的效果(多个形态).在代码上表现就是 同一个父类对象 赋予不同的子类对象 就 ...