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.
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.
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.
5 5 4 3 2 1 4 1 1 1 1
1 2 1 1 4 2
5 1 2 3 4 5 1 15
0 0
5 1 2 3 4 5 4 1 2 3 4
1 1 3 1
题意:给定两个序列,可以至多两次地选择第一个序列中的一个数和第二个序列中的一个数做交换
使得两序列的和之差的绝对值最小
思路:
分三种情况,一个是不交换,二是只交换一次(暴力),三是交换两次(预处理出所有两两之和,再二分)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#define max_ 2010
#define inf 0x3f3f3f3f
#define ll long long
#define les 1e-8
using namespace std;
struct node
{
int x,y;
ll w;
};
struct node num3[max_*max_],num4[max_*max_];
int n,m;
ll num1[max_],num2[max_];
bool cmp(const node &a,const node &b)
{
return a.w<b.w;
}
int main(int argc, char const *argv[]) {
// freopen("data.in","a+",stdin);
// freopen("data.out","w",stdout);
ll sum1=0,sum2=0;
int l1,l2,r1,r2;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lld",&num1[i]);
sum1+=num1[i];
}
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%lld",&num2[i]);
sum2+=num2[i];
}
int cnt=0;
ll minn=abs(sum1-sum2);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
ll d=abs((sum1+num2[j]-num1[i])-(sum2+num1[i]-num2[j]));
if(d<minn)
{
l1=i;
r1=j;
minn=d;
cnt=1;
}
}
}
if(cnt==0)
{
printf("%lld\n0\n",minn);
return 0;
}
int k1=1,k2=1;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
num3[k1].w=num1[i]+num1[j];
num3[k1].x=i;
num3[k1].y=j;
k1++;
}
}
for(int i=1;i<=m;i++)
{
for(int j=i+1;j<=m;j++)
{
num4[k2].w=num2[i]+num2[j];
num4[k2].x=i;
num4[k2].y=j;
k2++;
}
}
sort(num4+1,num4+k2,cmp);
ll minnn=abs(sum1-sum2);
for(int i=1;i<k1;i++)
{
int l=1,r=k2-1;
while(l<=r)
{
int mid=(l+r)>>1;
ll d=((sum1+num4[mid].w-num3[i].w)-(sum2+num3[i].w-num4[mid].w));
if(abs(d)<minnn)
{
minnn=abs(d);
l2=i;
r2=mid;
}
if(d>0)
r=mid-1;
else
l=mid+1;
}
}
if(minnn<minn)
{
printf("%lld\n2\n%d %d\n",minnn,num3[l2].x,num4[r2].x);
printf("%d %d\n",num3[l2].y,num4[r2].y);
}
else
{
printf("%lld\n1\n%d %d\n",minn,l1,r1);
}
return 0;
}