#include <bits/stdc++.h>

 using namespace std;

 const int maxn =  + ;

 const long long inf = 1e18;

 int n, m;

 long long suma, sumb;

 int a[maxn], b[maxn];

 long long dbl_a[maxn], dbl_b[maxn];

 long long sum_dbl_a[maxn * maxn + ], sum_dbl_b[maxn * maxn + ];

 int main()
{
map<int, int> pos_a;
scanf("%d", &n);
for(int i = ; i <= n; ++i){
scanf("%d", &a[i]);
suma += a[i];
dbl_a[i] = a[i] * ;
pos_a[dbl_a[i]] = i;
} map<long long, pair<int, int>> m_a;
map<int, long long> two_pos_a;
int cnt_a = , cnt_b = ;
for(int i = ; i <= n; ++i){
for(int j = i + ; j <= n; ++j){
m_a[dbl_a[i] + dbl_a[j]] = make_pair(i, j);
sum_dbl_a[++cnt_a] = dbl_a[i] + dbl_a[j];
}
}
scanf("%d", &m);
for(int i = ; i <= m; ++i){
scanf("%d", &b[i]);
sumb += b[i];
dbl_b[i] = b[i] * ;
} long long diff = (suma - sumb); if(diff == ) {
printf("0\n0\n");
return ;
} sort(dbl_a+, dbl_a++n); long long one_swap_ans = abs(suma - sumb);
long long two_swap_ans = abs(suma - sumb);
int one_swap_a = , one_swap_b = ;
int two_swap_a_1 = , two_swap_a_2 = , two_swap_b_1 = , two_swap_b_2 = ; for(int i = ; i <= m; ++i){
long long tmp = diff + dbl_b[i];
int pos = lower_bound(dbl_a+, dbl_a+n+, tmp) - dbl_a;
//printf("%d\n", pos);
long long big_res = inf;
if(pos <= n) big_res = dbl_a[pos-]; long long small_res = inf;
if(pos - >= ) small_res = dbl_a[pos - ]; if(abs(big_res - tmp) > abs(small_res - tmp)){
//printf("%d %d %d\n",pos_a[10], dbl_a[pos-1], pos);
long long temp = abs(small_res - tmp);
if(one_swap_ans > temp){
one_swap_ans = temp;
one_swap_a = pos_a[dbl_a[pos-]], one_swap_b = i;
}
} else {
long long temp = abs(big_res - tmp);
if(one_swap_ans > temp){
one_swap_ans = temp;
one_swap_a = pos_a[dbl_a[pos]], one_swap_b = i;
}
} } sort(sum_dbl_a + , sum_dbl_a + cnt_a + ); for(int i = ; i <= m; ++i){
for(int j = i + ; j <= m; ++j){
long long tmp = diff + dbl_b[i] + dbl_b[j];
int pos = lower_bound(sum_dbl_a+, sum_dbl_a+cnt_a+, tmp) - sum_dbl_a;
long long big_res = inf;
if(pos <= cnt_a) big_res = sum_dbl_a[pos]; long long small_res = inf;
if(pos - >= ) small_res = sum_dbl_a[pos - ]; if(abs(big_res - tmp) > abs(small_res - tmp)){
long long temp = abs(small_res - tmp); if(two_swap_ans > temp){
two_swap_ans = temp;
two_swap_a_1 = m_a[sum_dbl_a[pos-]].first;
two_swap_a_2 = m_a[sum_dbl_a[pos-]].second;
two_swap_b_1 = i;
two_swap_b_2 = j;
}
} else {
long long temp = abs(big_res - tmp); if(two_swap_ans > temp){
two_swap_ans = temp;
two_swap_a_1 = m_a[sum_dbl_a[pos]].first;
two_swap_a_2 = m_a[sum_dbl_a[pos]].second; two_swap_b_1 = i;
two_swap_b_2 = j;
}
} }
} long long ans = min(abs(diff), min(abs(one_swap_ans), abs(two_swap_ans)));
printf("%I64d\n", ans);
if(ans == diff){
printf("0\n");
} else if(one_swap_ans == ans){
printf("1\n");
printf("%d %d\n", one_swap_a, one_swap_b);
} else {
printf("2\n");
printf("%d %d\n", two_swap_a_1, two_swap_b_1);
printf("%d %d\n", two_swap_a_2, two_swap_b_2);
} return ;
}

只交换一次的情况
原来 |Suma - Sumb|
交换后 Suma - xi + yj - (sumb - yj + xi) = suma - sumb + 2(yj - xi) -> 0
则在确定xi的情况下 需要在 b序列里找到 最接近 xi 的 (suma - sumb) /2 + yj的 yj

交换两次的情况
原来 |suma - sumb|
交换后 Suma - xi - xj + yk + yl - (sumb + xi + xj - yk - yl) = suma - sumb + 2((yk + yl) - (xi + xj)) -> 0
则在确定 xi xj的情况下 需要在b序列里找到 最接近 xi + xj的 (suma - sumb)/2 + yk + yl的 yk 和 yl

codeforces 620D Professor GukiZ and Two Arrays的更多相关文章

  1. CodeForces 620D Professor GukiZ and Two Arrays 双指针

    Professor GukiZ and Two Arrays 题解: 将a数组都sort一遍之后, b数组也sort一遍之后. 可以观察得到 对于每一个ai来说, 整个数组bi是一个V型的. 并且对于 ...

  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. 修改linux内核启动logo及显示位置

    转载于:http://blog.chinaunix.net/uid-28458801-id-3484269.html 在此基础上我又添加了我的一些不同的地方,仅供参考 内核版本: 2.6.35.3 l ...

  2. Android 自定义带回调的Dialog 及EditText相关

      import android.app.Activity; import android.content.Context; import android.text.Editable; import ...

  3. 关于android中,菜单按钮点击事件首次执行之后再次执行需要双击按钮的问题

    有时候在获取事件的时候,需要双击才能获取,解决方法很简单,把返回值设为true,那么这个事件就不会再分发了,我预计是设为其他值会继续分发,造成事件的相应混乱

  4. solr增量数据配置说明

    转帖地址:http://www.blogjava.net/conans/articles/379546.html 以下资料整理自网络,觉的有必要合并在一起,这样方便查看.主要分为两部分,第一部分是对& ...

  5. linux终端后台运行

    nohup command &(然后X退出即可) &也可用来在终端中同时执行几条命令(并行,最后面不要忘记加&) command1 & command2 & c ...

  6. koa的跨域访问

    koa跨域访问:1.安装插件 npm install koa-cors --save-dev2.项目的app.js中var cors = require('koa-cors'); app.use(co ...

  7. ROS Learning-025 (提高篇-003 A Mobile Base-01) 控制移动平台

    ROS 提高篇 A Mobile Base-01 - 控制移动平台 - 基本知识 我使用的虚拟机软件:VMware Workstation 11 使用的Ubuntu系统:Ubuntu 14.04.4 ...

  8. c++ 类中模版成员函数

    C++函数模版与类模版. template <class T> void SwapFunction(T &first, T &second){ }//函数模版 templa ...

  9. ARC100D Equal Cut

    传送门 分析 首先我们想到的肯定是n^3暴力枚举,但这显然不行.然后我们想到的就是二分了,但这题没有什么单调性,所以二分也不行.这时候我就想到了先枚举找出p2的位置再在它的左右两边找到p1和p3,但是 ...

  10. CF 432B :Football Kit

    hash做法: #include<stdio.h> #include<string.h> ; int home[Max],away[Max],hash[Max]; int ma ...