Codeforces 620D

来源:互联网 发布:java类库是什么 编辑:程序博客网 时间:2024/06/05 10:32

寒假训练一:A题

传送门:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105080#problem/A

Professor GukiZ and Two Arrays
Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Professor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array asa as close as possible to the sum of the elements in the array bsb. 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.

Sample Input

Input
55 4 3 2 141 1 1 1
Output
121 14 2
Input
51 2 3 4 5115
Output
00
Input
51 2 3 4 541 2 3 4
Output
113 1


题意是有两个序列,可以不交换,交换一个数,交换两个数,使得两个数列和相减绝对值最小


不交换和交换一个比较简单直接枚举,时间分别为o(1)和o(m*n)

交换两个时,将第一个序列亮亮求和排序,枚举第二个序列的两个数,从第一个序列中二分查找最优解,时间o(m^2logn^2)

最后三个比一下就好了


20天前的题目...用小号交了20多次才过...

主要是cmp函数忘了打return...找人帮忙看还看不出来hhhhhhh

另外第一个序列只有一个数情况忘记特判了

一直以为是二分写错了

结果到头来只有二分写对了...

还是不够啊

下面代码

#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);}


0 0
原创粉丝点击