CodeForces 620D Professor GukiZ and Two Arrays 双指针
Professor GukiZ and Two Arrays
题解:
将a数组都sort一遍之后, b数组也sort一遍之后。
可以观察得到 对于每一个ai来说, 整个数组bi是一个V型的。
并且对于ai+1的最优解一定是在ai的右边。
然后我们将a数组 和 b数组枚举一遍。
然后再将a数组22组合, b数组22组合之后, 再枚举一遍。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 4e6 + ;
int a[N], b[N];
struct Node{
int v, l, r;
bool operator<(const Node & t) const{
return v < t.v;
}
}A[N], B[N];
LL ans;
pll ansa, ansb;
LL suma = , sumb = ;
int f;
LL cal(int i, int j){
LL tsuma = suma - A[i].v + B[j].v;
LL tsumb = sumb - B[j].v + A[i].v;
return abs(tsuma - tsumb);
}
void Find(int n, int m){
if(!n || !m) return ;
sort(A+, A++n); sort(B+, B++m);
for(int i = , j = ; i <= n; ++i){
while((j+) <= m && cal(i,j) >= cal(i,j+)) ++j;
if(ans > cal(i,j)){
ans = cal(i, j);
ansa = pll(A[i].l, B[j].l);
ansb = pll(A[i].r, B[j].r);
}
}
}
int main(){
int n, m;
scanf("%d", &n);
f += (n==);
for(int i = ; i <= n; ++i){
scanf("%d", &a[i]);
A[i] = {a[i], i, };
suma += a[i];
}
scanf("%d", &m);
for(int i = ; i <= m; ++i){
scanf("%d", &b[i]);
B[i] = {b[i], i, };
sumb += b[i];
}
ans = abs(suma - sumb); ansa = ansb = {, };
Find(n, m);
int atot = , btot = ;
for(int i = ; i <= n; ++i){
for(int j = i+; j <= n; ++j){
A[++atot] = {a[i]+a[j], i, j};
}
}
for(int i = ; i <= m; ++i){
for(int j = i+; j <= m; ++j){
B[++btot] = {b[i]+b[j], i, j};
}
}
Find(atot, btot);
printf("%lld\n", ans);
if(ansa.fi == ){
puts("");
}
else if(ansb.se == ){
puts("");
printf("%d %d\n", ansa.fi, ansa.se);
}
else {
puts("");
printf("%d %d\n", ansa.fi, ansa.se);
printf("%d %d\n", ansb.fi, ansb.se);
}
return ;
}
CodeForces 620D Professor GukiZ and Two Arrays 双指针的更多相关文章
- codeforces 620D Professor GukiZ and Two Arrays
#include <bits/stdc++.h> using namespace std; + ; const long long inf = 1e18; int n, m; long l ...
- Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays 二分
D. Professor GukiZ and Two Arrays 题目连接: http://www.codeforces.com/contest/620/problem/D Description ...
- Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays
Professor GukiZ and Two Arrays 题意:两个长度在2000的-1e9~1e9的两个序列a,b(无序);要你最多两次交换元素,使得交换元素后两序列和的差值的绝对值最小:输出这 ...
- 【CodeForces 620D】Professor GukiZ and Two Arrays
题 题意 两个数列,一个有n个数,另一个有m个数,让你最多交换两次两个数列的数,使得两个数列和的差的绝对值最小,求这个差的绝对值.最少交换次数.交换数对 分析 交换0次.1次可得到的最小的差可以枚举出 ...
- CodeForces 620A Professor GukiZ's Robot
水题 #include<cstdio> #include<cstring> #include<cmath> #include<stack> #inclu ...
- Educational Codeforces Round 6 A. Professor GukiZ's Robot 水
A. Professor GukiZ's Robot Professor GukiZ makes a new robot. The robot are in the point with coor ...
- 【24.67%】【codeforces 551C】 GukiZ hates Boxes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【74.89%】【codeforces 551A】GukiZ and Contest
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 551 C GukiZ hates Boxes
--睡太晚了. ..脑子就傻了-- 这个题想的时候并没有想到该这样-- 题意大概是有n堆箱子从左往右依次排列,每堆ai个箱子,有m个人,最開始都站在第一个箱子的左边, 每个人在每一秒钟都必须做出两种选 ...
随机推荐
- Hadoop MapReduce的Shuffle过程
一.概述 理解Hadoop的Shuffle过程是一个大数据工程师必须的,笔者自己将学习笔记记录下来,以便以后方便复习查看. 二. MapReduce确保每个reducer的输入都是按键排序的.系统执行 ...
- 微服务SpringCloud之Spring Cloud Config配置中心Git
微服务以单个接口为颗粒度,一个接口可能就是一个项目,如果每个项目都包含一个配置文件,一个系统可能有几十或上百个小项目组成,那配置文件也会有好多,对后续修改维护也是比较麻烦,就和前面的服务注册一样,服务 ...
- UR机器人的位姿
一.Ur 移动命令 UR机器人移动,一共有三种移动指令,movej,movel,movep,分别是关节运动,线性运动,圆周运动. movej:6个关节的弧度 movel/movep:分别是x,y,z, ...
- Axis1.4 配置数组类型复杂对象
最近 项目对接 webservice,要求SOAP 标准是1.1,然后在axis 和 spring ws 和 cxf 之间进行选择,然后axis 可以自定义服务,然后随tomcat启动发布,sprin ...
- 比特平面分层(一些基本的灰度变换函数)基本原理及Python实现
1. 基本原理 在灰度图中,像素值的范围为[0, 255],即共有256级灰度.在计算机中,我们使用8比特数来表示每一个像素值.因此可以提取出不同比特层面的灰度图.比特层面分层可用于图片压缩:只储存较 ...
- 11、增强型for循环对二维数组的输出(test8.java)
由于笔者原因,这部分知识,尚不能整理出代码,笔者会好好学习增强型for循环中迭代起的相关知识,在笔者有能力,书写好这段代码后,将对本篇文章,进行二次修改,也同时欢迎大家与笔者交流,共同学习,共同进步. ...
- mac 下 docker 镜像加速器
配置镜像加速器 具体设置见下图即可. 
- HTML/CSS:div居中和div内部元素垂直居中(1)
div居中 div水平和垂直居中,text-align和vertical-align不起作用,因为标签div没有这两个属性,所以再css中设置这两个值不能居中的效果 1. div水平居中:设置marg ...
- .netcore持续集成测试篇之搭建内存服务器进行集成测试一
系列目录 在web项目里,我们把每一层的代码的单元测试都通过并不代表程序能正常运行,因为这个过程缺失了http管道,很多时候我们还还需要把项目布在iis环境中或者在vs里启动iis express服务 ...
- HomeKit智能球泡
产品名称: 智能LED灯泡调光调色 接入苹果HomeKit家庭(无需网关).天猫精灵.小爱.小度.Google.ALEXA 产品价格:9.9 本产品是针对HomeKit的产品,没有iphone手机,配 ...