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 双指针的更多相关文章

  1. codeforces 620D Professor GukiZ and Two Arrays

    #include <bits/stdc++.h> using namespace std; + ; const long long inf = 1e18; int n, m; long l ...

  2. 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 ...

  3. Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays

    Professor GukiZ and Two Arrays 题意:两个长度在2000的-1e9~1e9的两个序列a,b(无序);要你最多两次交换元素,使得交换元素后两序列和的差值的绝对值最小:输出这 ...

  4. 【CodeForces 620D】Professor GukiZ and Two Arrays

    题 题意 两个数列,一个有n个数,另一个有m个数,让你最多交换两次两个数列的数,使得两个数列和的差的绝对值最小,求这个差的绝对值.最少交换次数.交换数对 分析 交换0次.1次可得到的最小的差可以枚举出 ...

  5. CodeForces 620A Professor GukiZ's Robot

    水题 #include<cstdio> #include<cstring> #include<cmath> #include<stack> #inclu ...

  6. 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 ...

  7. 【24.67%】【codeforces 551C】 GukiZ hates Boxes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. 【74.89%】【codeforces 551A】GukiZ and Contest

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. codeforces 551 C GukiZ hates Boxes

    --睡太晚了. ..脑子就傻了-- 这个题想的时候并没有想到该这样-- 题意大概是有n堆箱子从左往右依次排列,每堆ai个箱子,有m个人,最開始都站在第一个箱子的左边, 每个人在每一秒钟都必须做出两种选 ...

随机推荐

  1. 【MySQL】服务无法启动(Mac)

    如图所示: 点击 Start MySQL Server 没反应-- 终端输入 mysql 命令时报错如下: ERROR 2002 (HY000): Can't connect to local MyS ...

  2. 华为matebook14vm虚拟机错误

    1.创建时显示不支持64位虚拟机 测试环境: 华为matebook14 window10 专业工作站版  1903   问题描述: 创建虚拟机时显示:此主机不支持64位解决方案   问题参考: 参考1 ...

  3. zabbix监控WEB网站性能

    一直在纠结用什么实例来给大家演示呢?想来想去还是官方的好,那我们怎么用zabbix监控web性能和可用性呢?我们这边分为几个步骤:打开网站.登陆.登陆验证.退出,一共4个小step,看实例. 检测流程 ...

  4. macOS 安装配置yaf框架 生成yaf项目

    macOS 安装配置yaf框架 Yaf只支持PHP5.2及以上的版本. 并支持最新的PHP5.3.3 Yaf需要SPL的支持. SPL在PHP5中是默认启用的扩展模块 Yaf需要PCRE的支持. PC ...

  5. spring-boot-starter-quartz集群实践

    [**前情提要**]由于项目需要,需要一个定时任务集群,故此有了这个spring-boot-starter-quartz集群的实践.springboot的版本为:2.1.6.RELEASE:quart ...

  6. rtags——node.js+redis实现的标签管理模块

    引言在我们游览网页时,随处可见标签的身影: 进入个人微博主页,可以看到自己/他人的标签,微博系统会推送与你有相同标签的人 游览博文,大多数博文有标签标记,以说明文章主旨,方便搜索和查阅 网上购物,我们 ...

  7. 基于 WPF 模块化架构下的本地化设计实践

    背景描述 最近接到一个需求,就是要求我们的 WPF 客户端具备本地化功能,实现中英文多语言界面.刚开始接到这个需求,其实我内心是拒绝的的,但是没办法,需求是永无止境的.所以只能想办法解决这个问题. 首 ...

  8. 算法与数据结构基础 - 分治法(Divide and Conquer)

    分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...

  9. 汇总VSCode中比较好用的插件

    使用vscode编辑器两年的时间,总结出前端一些比较方便的插件 1. Auto Close Tag 自动添加HTML / XML关闭标签 2. Auto Complete Tag 自动完成标签 3 A ...

  10. 迁移学习(Transformer),面试看这些就够了!(附代码)

    1. 什么是迁移学习 迁移学习(Transformer Learning)是一种机器学习方法,就是把为任务 A 开发的模型作为初始点,重新使用在为任务 B 开发模型的过程中.迁移学习是通过从已学习的相 ...