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个人,最開始都站在第一个箱子的左边, 每个人在每一秒钟都必须做出两种选 ...
随机推荐
- Linux 查看一个端口的连接数
netstat -antp|grep -i "80" |wc -l 譬如查看80端口的连接数
- 下拉框value ,selectedIndex
- 【总结整理】WMS、WMTS、WFS
参考:http://www.cnblogs.com/naaoveGIS/p/5508882.html WMTS:WMTS是OGC制定的一种发布瓦块地图的Web服务规范,wms主要是动态地图,wmts是 ...
- strstr()查找函数,strchr(),strrchr(),stristr()/strpos(),strrpos()查找字符串位置
在一个较长的字符串这查找匹配的字符串或字符,其中strstr()和strchr()是完全一样的. 例: echo strstr('why always you','you'); 输出: you 如果为 ...
- Tensorflow 优化学习
# coding: utf-8 import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data pr ...
- git push是报Permission denied (publickey)错误解决
今天晕了半天了,搞了个git工程到github上,以为很简单,因为之前也弄过,那知道搞了大半天都搞不好,一直报如下错误 D:\javawork\ee-0.0.1-SNAPSHOT>git pus ...
- 高性能MySQL笔记-第5章Indexing for High Performance-001B-Tree indexes(B+Tree)
一. 1.什么是B-Tree indexes? The general idea of a B-Tree is that all the values are stored in order, and ...
- python-ASCII与Unicode
# Auther: Aaron Fan'''ASCII:不支持中文,1个英文占1个字节Unicode(万国码,支持所有国家的文字显示):支持中文,但是每个英文和中文都占2个字节UTF-8(是一种针对U ...
- xe6 android控件透明度设置方法
今天才知道xe6 android控件的透明度设置方法:只需设置控件中的Opacity参数,默认为1--不透明 panel1.Opacity:=0.60;
- ipmitool查询服务器功耗
通过ipmitool查看服务器功耗 ipmitool -H $ip -I lanplus -U $user -P $password sdr elist | grep "Pwr Consum ...