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个人,最開始都站在第一个箱子的左边, 每个人在每一秒钟都必须做出两种选 ...
随机推荐
- Java 访问 Kylin 总结
这次开发功能是OEM统计报表.统计报表的数据由大数据平台部的同事收集,数据的展示由我们部门开发. 大数据那边使用 Kylin 分布式分析引擎(kylin官方文档). Kylin 虽比较偏向大数据相关, ...
- 长城防火墙(GFW)
一.简介 中国防火长城,官方名为金盾工程,是由政府运作的一个互联网审查监控项目.在其管辖互联网内部建立的多套网络审查系统的总称,包括相关行政审查系统.其英文名称Great Firewall of Ch ...
- R: 常用操作:
################################################### #清除所有变量: rm(list=ls()) #查看变量类型 getwd() setwd() i ...
- 100722E The Bookcase
传送门 题目大意 给你一些书的高度和宽度,有一个一列三行书柜,要求放进去书后,三行书柜的高的和乘以书柜的宽度最小.问这个值最小是多少. 分析 我们可以先将所有书按照高度降序排好,这样对于每一层只要放过 ...
- 《Head First Servlets & JSP》-6-会话管理
容器怎么知道客户是谁 Http协议是无状态连接,客户浏览器与服务器建立连接.发出请求.得到响应,然后关闭连接.即,连接只针对一个请求/响应. 对容器而言,每个请求都来自于一个新的客户. 客户需要一个唯 ...
- 关于setVisibility的几个常量
在xml文件中,view控件一般都会有android:visibility这个属性 android:visibility:gone|cisible|invisible 在代码中,可以通过方法setVi ...
- [转]Linux安装前配置操作记录
转至:http://m.blog.csdn.net/weixin_35884835/article/details/52385077 1.修改用户的SHELL的限制,修改/etc/security/l ...
- msbuild 中文说明文档
Microsoft (R) 生成引擎版本 14.0.25420.1 版权所有(C) Microsoft Corporation.保留所有权利. 语法: MSBuild.exe [选项] [项目文件] ...
- code manager toos TotoiseSVN解锁
TotoiseSVN解锁 一.出现的原因: 1.是开发人员手动锁定了. 2.是由于版本错乱造成的锁定. 二.解决方案: 1.使用客户端解决方案: (1)删除本地副本,重新获取文件. (2)可以先尝试c ...
- Django会话,用户和注册之cookie
HTTP状态和TCP不一样,HTTP是无状态的,也就是这一次请求和下一次请求之间没有任何状态保持,我们无法根据请求例如IP来识别是否在同一人的连续性请求.就像我们在访问网站的时候,输入了用户名和密码, ...