【Educational Codeforces Round 6D】【暴力 SET二分】Professor GukiZ and Two Arrays

D. Professor GukiZ and Two Arrays
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa as close as possible to the sum of the elements in the array b sb. So he wants to minimize the value v = |sa - sb|.

In one operation professor can swap some element from the array a and some element from the array b. For example if the array a is[5, 1, 3, 2, 4] and the array b is [3, 3, 2] professor can swap the element 5 from the array a and the element 2 from the array b and get the new array a [2, 1, 3, 2, 4] and the new array b [3, 3, 5].

Professor doesn't want to make more than two swaps. Find the minimal value v and some sequence of no more than two swaps that will lead to the such value v. Professor makes swaps one by one, each new swap he makes with the new arrays a and b.

Input

The first line contains integer n (1 ≤ n ≤ 2000) — the number of elements in the array a.

The second line contains n integers ai ( - 109 ≤ ai ≤ 109) — the elements of the array a.

The third line contains integer m (1 ≤ m ≤ 2000) — the number of elements in the array b.

The fourth line contains m integers bj ( - 109 ≤ bj ≤ 109) — the elements of the array b.

Output

In the first line print the minimal value v = |sa - sb| that can be got with no more than two swaps.

The second line should contain the number of swaps k (0 ≤ k ≤ 2).

Each of the next k lines should contain two integers xp, yp (1 ≤ xp ≤ n, 1 ≤ yp ≤ m) — the index of the element in the array a and the index of the element in the array b in the p-th swap.

If there are several optimal solutions print any of them. Print the swaps in order the professor did them.

Examples
input
5
5 4 3 2 1
4
1 1 1 1
output
1
2
1 1
4 2
input
5
1 2 3 4 5
1
15
output
0
0
input
5
1 2 3 4 5
4
1 2 3 4
output
1
1
3 1


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 2020, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n,m;
int a[N], b[N];
int ansk; LL ansv;
int P[6];
void update(int k, LL v,int p1,int p2,int p3,int p4)
{
	v = abs(v);
	if (v < ansv)
	{
		ansv = v;
		ansk = k;
		P[1] = p1;
		P[2] = p2;
		P[3] = p3;
		P[4] = p4;
	}
}
struct B
{
	LL v;
	int p2, p4;
	B() {}
	B(LL v_, int p2_, int p4_) { v = v_; p2 = p2_; p4 = p4_; }
	bool operator < (const B&b)const
	{
		return v < b.v;
	}
};
set<B>sot;
set<B>::iterator it;
int main()
{
	while(~scanf("%d",&n))
	{
		ansv = 1e18;
		LL suma = 0, sumb = 0;
		for (int i = 1; i <= n; ++i)scanf("%d", &a[i]), suma += a[i];
		scanf("%d", &m);
		for (int i = 1; i <= m; ++i)scanf("%d", &b[i]), sumb += b[i];
		update(0, suma - sumb,0,0,0,0);
		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= m; ++j)
			{
				update(1,(suma-a[i]*2)-(sumb-b[j]*2),i,j,0,0);
			}
		}
		sot.clear();
		for (int i = 1; i < m; ++i)
		{
			for (int j = i + 1; j <= m; ++j)
			{
				sot.insert(B(sumb - b[i]*2 - b[j]*2, i, j));
			}
		}
		for (int i = 1; i <= n; ++i)
		{
			for (int j = i + 1; j <= n; ++j)
			{
				it = sot.lower_bound(B(suma - a[i] * 2 - a[j] * 2, -1, -1));
				if (it != sot.end())update(2, (suma - a[i] * 2 - a[j] * 2 - it->v), i, it->p2, j, it->p4);
				if (it != sot.begin())
				{
					--it;
					update(2, (suma - a[i] * 2 - a[j] * 2 - it->v), i, it->p2, j, it->p4);
				}
			}
		}
		printf("%lld\n", ansv);
		printf("%d\n", ansk);
		for (int i = 1; i <= ansk; ++i)
		{
			printf("%d %d\n", P[i*2-1], P[i*2]);
		}
	}
	return 0;
}
/*
【题意】
给你两个数列a[],b[](1<=a[],b[]<=1e9)
长度分别为n,m(1<=n,m<=2000)
我们可以选择(0~2)个pair,做两个数列的元素交换。
问你如何交换,可以使得最后abs(suma-sumb)尽可能小

【类型】
暴力

【分析】
只是k==2的情况稍难处理。
我们枚举第一个数列哪两个(i,j)和第二个数列做了交换。
然后对于第二个数列,一样枚举两个交换位置x,y。

我们发现交换后的abs()==abs( (suma-a[i]*2-a[j]*2)-(sumb-b[x]*2-b[y]*2) )
于是我们把(sumb-b[x]*2-b[y]*2,x,y)存进set中
然后二分(suma-a[i]*2-a[j]*2),更新答案。

【时间复杂度&&优化】
O(n^2log(n^2))

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值