Professor GukiZ and Two Arrays
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
题意是有两个序列,可以不交换,交换一个数,交换两个数,使得两个数列和相减绝对值最小
不交换和交换一个比较简单直接枚举,时间分别为o(1)和o(m*n)
交换两个时,将第一个序列亮亮求和排序,枚举第二个序列的两个数,从第一个序列中二分查找最优解,时间o(m^2logn^2)
最后三个比一下就好了
题意是有两个序列,可以不交换,交换一个数,交换两个数,使得两个数列和相减绝对值最小
不交换和交换一个比较简单直接枚举,时间分别为o(1)和o(m*n)
交换两个时,将第一个序列亮亮求和排序,枚举第二个序列的两个数,从第一个序列中二分查找最优解,时间o(m^2logn^2)
最后三个比一下就好了
code:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<functional>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
__int64 a[2005];
__int64 b[2005];
struct node
{
int l,r;
__int64 h;
}db[2000005];
bool cmp(node a,node b)
{
if(a.h==b.h && a.l==b.l) return a.r<b.r;
else if(a.h==b.h) return a.l<b.l;
return a.h<b.h;
}
__int64 qfabs(__int64 a)
{
if(a<0) a=-a;
return a;
}
int main()
{
int i,j,k;
int n,m;
__int64 suma,sumb;
int ii,jj;
int l,r,mid;
__int64 ans0,ans1,ans2,ans;
int a1,a2,b1,b2;
suma=sumb=0;
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%I64d",&a[i]);
suma+=a[i];
}
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%I64d",&b[i]);
sumb+=b[i];
}
ans0=qfabs(suma-sumb);
ans1=999999999;
ans1=ans1*10000;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
suma=suma-a[i]+b[j];
sumb=sumb-b[j]+a[i];
if(ans1>qfabs(suma-sumb))
{
ans1=qfabs(suma-sumb);
ii=i;
jj=j;
}
suma=suma+a[i]-b[j];
sumb=sumb+b[j]-a[i];
}
k=1;
for(i=1;i<m;i++)
for(j=i+1;j<=m;j++)
{
db[k].l=i;
db[k].r=j;
db[k].h=(a[i]+a[j])*2;
k++;
}
sort(db+1,db+k,cmp);
ans2=999999999;
ans2=ans2*10000;
if(k>1)//防止只有一个点
{
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
ans=suma+2*(b[i]+b[j])-sumb;
l=1;
r=k-1;
while(l<r-1)
{
mid=(l+r)/2;
if(db[mid].h==ans)
{
l=r=mid;
break;
}
if(db[mid].h>ans) r=mid;
if(db[mid].h<ans) l=mid;
}
if(qfabs(ans-db[l].h)>qfabs(db[r].h-ans) && qfabs(db[r].h-ans)<ans2)
{
b1=i;
b2=j;
a1=db[r].l;
a2=db[r].r;
ans2=qfabs(db[r].h-ans);
}
else if(qfabs(ans-db[l].h)<=qfabs(db[r].h-ans) && qfabs(db[l].h-ans)<ans2)
{
b1=i;
b2=j;
a1=db[l].l;
a2=db[l].r;
ans2=qfabs(db[l].h-ans);
}
}
}
if(ans0<=ans1 && ans0<=ans2) printf("%d\n0\n",ans0);
else if(ans1<=ans2) printf("%I64d\n1\n%d %d\n",ans1,ii,jj);
else printf("%I64d\n2\n%d %d\n%d %d\n",ans2,a1,b1,a2,b2);
return 0;
}