题目大意: 给你n个数字, 定义不连贯值为, max(abs(a[ i ] - b[ i ])) ,现在让你把m个新的数字插入n + 1 个空位中,使得不连贯值最小。

思路:二分不连贯值, 每次进行二分图匹配, 注意进行二分图匹配的时候需要加入虚拟的点,因为这n + 1个空位中有些点是必须加数字的。

 #include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int>
using namespace std; const int N=+;
const int M=+;
const int inf=0x3f3f3f3f;
const LL INF=0x3f3f3f3f3f3f3f3f;
const int mod=1e9 + ; int n, m, a[N], b[N],cx[N], cy[N], vis[N];
vector<int> edge[N]; bool path(int u)
{
for(int v : edge[u])
{
if(!vis[v])
{
vis[v] = ;
if(cy[v] == - || path(cy[v]))
{
cx[u] = v;
cy[v] = u;
return true;
}
}
}
return false;
} bool maxmatch()
{
int res=;
memset(cx, -, sizeof(cx));
memset(cy, -, sizeof(cy));
for(int i = ; i <= * n + ; i++)
{
if(cx[i] == -)
{
memset(vis, , sizeof(vis));
if(path(i)) continue;
else return false;
}
}
return true;
} bool check(int x) {
for(int i = ; i <= * n + ; i++)
edge[i].clear();
for(int i = ; i <= n + ; i++) {
if(i == ) {
for(int j = ; j <= m; j++) {
if(abs(b[j] - a[]) <= x) {
edge[i].push_back(n + + j);
edge[n + + j].push_back(i);
}
}
for(int j = m + ; j <= n + ; j++) {
edge[i].push_back(n + + j);
edge[n + + j].push_back(i);
}
} else if(i == n + ) {
for(int j = ; j <= m; j++) {
if(abs(b[j] - a[n]) <= x) {
edge[i].push_back(n + + j);
edge[n + + j].push_back(i);
}
}
for(int j = m + ; j <= n + ; j++) {
edge[i].push_back(n + + j);
edge[n + + j].push_back(i);
}
} else {
for(int j = ; j <= m; j++) {
if((abs(b[j] - a[i]) <= x) && abs(b[j] - a[i - ]) <= x) {
edge[i].push_back(n + + j);
edge[n + + j].push_back(i);
}
}
if(abs(a[i] - a[i - ]) <= x) {
for(int j = m + ; j <= n + ; j++) {
edge[i].push_back(n + + j);
edge[n + + j].push_back(i);
}
}
}
}
return maxmatch();
}
int main() {
int T; scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++)
scanf("%d", &a[i]);
for(int i = ; i <= m; i++)
scanf("%d", &b[i]); int l = , r = 1e9, ans = -; while(l <= r) {
int mid = (l + r) >> ;
if(check(mid)) {
ans = mid;
r = mid - ;
} else {
l = mid + ;
}
}
printf("%d\n", ans);
}
return ;
}
/***************
****************/

上海高校金马五校赛 J - 小Y写文章的更多相关文章

  1. “盛大游戏杯”第15届上海大学程序设计联赛夏季赛暨上海高校金马五校赛题解&&源码【A,水,B,水,C,水,D,快速幂,E,优先队列,F,暴力,G,贪心+排序,H,STL乱搞,I,尼姆博弈,J,差分dp,K,二分+排序,L,矩阵快速幂,M,线段树区间更新+Lazy思想,N,超级快速幂+扩展欧里几德,O,BFS】

    黑白图像直方图 发布时间: 2017年7月9日 18:30   最后更新: 2017年7月10日 21:08   时间限制: 1000ms   内存限制: 128M 描述 在一个矩形的灰度图像上,每个 ...

  2. 上海高校金马五校赛 F题:1 + 2 = 3?

    链接:https://www.nowcoder.com/acm/contest/91/F来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言26214 ...

  3. 埃森哲杯第十六届上海大学程序设计联赛春季赛暨上海高校金马五校赛 C序列变换

    链接:https://www.nowcoder.com/acm/contest/91/C来源:牛客网没有账号的同学这样注册,支持博主 题目描述 给定两个长度为n的序列,ai, bi(1<=i&l ...

  4. K-序列(埃森哲杯第十六届上海大学程序设计联赛春季赛暨上海高校金马五校赛)

    题目描述 给一个数组 a,长度为 n,若某个子序列中的和为 K 的倍数,那么这个序列被称为“K 序列”.现在要你 对数组 a 求出最长的子序列的长度,满足这个序列是 K 序列.  输入描述: 第一行为 ...

  5. 2017Summmer_上海金马五校 F题,G题,I题,K题,J题

    以下题目均自己搜 F题  A序列 一开始真的没懂题目什么意思,还以为是要连续的子串,结果发现时序列,简直智障,知道题意之后,好久没搞LIS,有点忘了,复习一波以后,直接双向LIS,处理处两个数组L和R ...

  6. 2017年上海金马五校程序设计竞赛:Problem B : Sailing (广搜)

    Description Handoku is sailing on a lake at the North Pole. The lake can be considered as a two-dime ...

  7. 2017年上海金马五校程序设计竞赛:Problem K : Treasure Map (蛇形填数)

    Description There is a robot, its task is to bury treasures in order on a N × M grids map, and each ...

  8. 2017年上海金马五校程序设计竞赛:Problem I : Frog's Jumping (找规律)

    Description There are n lotus leaves floating like a ring on the lake, which are numbered 0, 1, ..., ...

  9. 2017年上海金马五校程序设计竞赛:Problem E : Find Palindrome (字符串处理)

    Description Given a string S, which consists of lowercase characters, you need to find the longest p ...

随机推荐

  1. 【Revit API】脱离中心文件

    话不多说,直接代码,整个过程不需要发起Transaction OpenOptions op = new OpenOptions(); op.Audit = true; //是否需要核查 op.Deta ...

  2. Codeforces 817

    A 你可以按如下方式移动 问能不能从给定的一个坐标走到另一个. [solution] 裸,奇偶性注意 #include<stdio.h> #include<stdlib.h> ...

  3. POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom /ZOJ 1291 MPI Maelstrom (最短路径)

    POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom ...

  4. 【CF884D】Boxes And Balls k叉哈夫曼树

    题目大意:给定一个大小为 N 的集合,每次可以从中挑出 2 个或 3 个数进行合并,合并的代价是几个数的权值和,求将这些数合并成 1 个的最小代价是多少. 引理:K 叉哈夫曼树需要保证 \((n-1) ...

  5. 【POJ2631】Roads in the North 树的直径

    题目大意:给定一棵 N 个节点的边权无根树,求树的直径. 代码如下 #include <cstdio> #include <algorithm> using namespace ...

  6. MySQL-->高级-->001-->MySQL备份与恢复测试

  7. shell脚本中自定义日志记录到文件

    自定义日志函数和前期变量 # adirname - return absolute dirname of given file adirname() { odir=`pwd`; cd `dirname ...

  8. Spring整合Quartz定时任务 在集群、分布式系统中的应用(Mysql数据库环境)

    Spring整合Quartz定时任务 在集群.分布式系统中的应用(Mysql数据库环境)   转载:http://www.cnblogs.com/jiafuwei/p/6145280.html 单个Q ...

  9. Dubbo学习笔记4:服务消费端泛化调用与异步调用

    本文借用dubbo.learn的Dubbo API方式来解释原理. 服务消费端泛化调用 前面我们讲解到,基于Spring和基于Dubbo API方式搭建简单的分布式系统时,服务消费端引入了一个SDK二 ...

  10. Codeforces Round #481 (Div. 3) G. Petya's Exams

    http://codeforces.com/contest/978/problem/G 感冒是真的受不了...敲代码都没力气... 题目大意: 期末复习周,一共持续n天,有m场考试 每场考试有如下信息 ...