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 题目大 ...
随机推荐
- C:clock() 计算代码执行时间
clock():捕捉从程序开始运行到clock()被调用时所耗费的事件. 这个时间的单位是 clock tick,即时钟打点 常数 CLK_TCK:机器时钟每秒走的时钟打点数 要使用这个函数需要包含头 ...
- 洛谷P1192台阶问题(DP)
题目描述 有NNN级的台阶,你一开始在底部,每次可以向上迈最多KKK级台阶(最少111级),问到达第NNN级台阶有多少种不同方式. 输入格式 两个正整数N,K. 输出格式 一个正整数,为不同方式数,由 ...
- svnserve: Can’t bind server socket: Address already in use报错解决办法
最近在学习自己搭建SVN服务,意外的报错 svnserve: Can’t bind server socket: Address already in use 于是google了下,原来是 已经启动了 ...
- NET站点升级后,新特新无法编译通过
NET3.5 webconfig中有自动配置如下代码,用于指示编译器. <system.codedom> <compilers> <compiler language=& ...
- 《JavaScript高级程序设计》读书笔记(二)在html中使用JavaScript
主要内容---使用<script>元素---嵌入脚本与外部脚本---文档模式对JavaScript的影响---考虑禁用JavaScript的场景 <script>元素---向h ...
- ThinkPHP6源码:从Http类的实例化看依赖注入是如何实现的
ThinkPHP 6 从原先的 App 类中分离出 Http 类,负责应用的初始化和调度等功能,而 App 类则专注于容器的管理,符合单一职责原则. 以下源码分析,我们可以从 App,Http 类的实 ...
- 语言国际化:中文ASC码互转
https://javawind.net/tools/native2ascii.jsp 1.首先找到了上面的链接,也就是下图,输入中文就可立即得出ASCII码 2.看到上图第一条,找到了JDK/bin ...
- 1.ORM介绍,基本配置及通过ORM框架创建表
1.介绍 ORM全拼Object-Relation Mapping(对象-关系映射) 作用:主要实现模型对象到关系数据库数据的映射 通过ORM框架作为一个中间者或者是一个桥梁,开发者通过定义模型类,属 ...
- javascript入门教程01
1.javascript中变量的声明和赋值的三种方式 (1)先声明后赋值 var width; width=5; (2)同时声明和赋值变量 var width=5; var x,y,z=10; (3) ...
- 最大流EK板子
#include <bits/stdc++.h> using namespace std; ; const int INF=0x7fffffff; typedef long long ll ...