codeforces 620D Professor GukiZ and Two Arrays
#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的更多相关文章
- CodeForces 620D Professor GukiZ and Two Arrays 双指针
Professor GukiZ and Two Arrays 题解: 将a数组都sort一遍之后, b数组也sort一遍之后. 可以观察得到 对于每一个ai来说, 整个数组bi是一个V型的. 并且对于 ...
- 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个人,最開始都站在第一个箱子的左边, 每个人在每一秒钟都必须做出两种选 ...
随机推荐
- 【WebRTC】简介
WebRTC 名称源自网页实时通信(英语:Web Real-Time Communication)的缩写,是一个支持网页浏览器进行实时语音对话或视频对话的API.它于2011年6月1日开源并在Goog ...
- 数据库开源框架ormlite
今天听说了ORM框架ORMLITE,特地去了解了一下. 该框架可以使用注解方式来生成数据库表,还封装了常用的数据库操作. 类似J2EE的HIBERNATE框架对数据库的处理. 省去了书写建表语句的麻烦 ...
- Ros学习service——小海龟
rosservice 服务(services)是节点之间通讯的另一种方式.服务允许节点发送请求(request) 并获得一个响应(response) rosservice list 输出可用服务的信息 ...
- 算法Sedgewick第四版-第1章基础-016一list
import java.util.Iterator; import java.util.NoSuchElementException; public class List<Item> im ...
- 算法Sedgewick第四版-第1章基础-013一用stack实现自动补全表达式括号
package algorithms.exercise; import algorithms.ADT.Stack; import algorithms.util.StdIn; import algor ...
- 4、Brief primer and lexicon for PacBio SMRT sequencing
转载:http://pacbiofileformats.readthedocs.io/en/5.1/Primer.html 转载:http://pacbiofileformats.readthedoc ...
- Python程序设计7——文件读写
1 文件读写简介 文件读写是应用程序中的常用操作.下面介绍Python中进行文件读写.Python的文件读写是非常简单的. 1.1 open函数 open函数一般有了两个必须参数,一个是文件名参数,另 ...
- 数据结构_just_sort
问题描述 给定两个序列 a b,序列 a 原先是一个单调递增的正数序列,但是由于某些原因,使得序列乱序了,并且一些数丢失了(用 0 表示).经过数据恢复后,找到了正数序列 b ,且序列 a 中 0 的 ...
- Web开发秘方(WEB DEVELOPMENT RECIPES)[47.5MB] PDF扫描版
不借助插件怎样在移动设备上实现动画效果?怎样快速搭建HTML电子邮箱?怎样制作跨PC和移动设备显示的应用界面?怎样利用最新的JavaScript框架提高应用的响应速度?怎样有效利用CoffeeScri ...
- [转]Marshaling a SAFEARRAY of Managed Structures by P/Invoke Part 4.
1. Introduction. 1.1 In parts 1 through 3 of this series of articles, I have thoroughly discussed th ...