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 ais [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 band 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
调换次数最大为2,所以直接暴力会超时,所以把序列的任意两个数的和保存下来,再对每个序列的两数的和进行调换,就基本相当于只调换一次了。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <stack> #include <bitset> #include <queue> #include <set> #include <map> #include <string> #include <algorithm> #define Si(a) scanf("%d",&a) #define Sl(a) scanf("%lld",&a) #define Sd(a) scanf("%lf",&a) #define Ss(a) scanf("%s",a) #define Pi(a) printf("%d\n",(a)) #define Pl(a) printf("%lld\n",(a)) #define Pd(a) printf("%lf\n",(a)) #define Ps(a) printf("%s\n",(a)) #define W(a) while(a--) #define mem(a,b) memset(a,(b),sizeof(a)) #define FOP freopen("data.txt","r",stdin) #define inf 0x3f3f3f3f #define maxn 1000010 #define mod 1000000007 #define PI acos(-1.0) #define LL long long using namespace std; typedef pair<int,int > PII; vector<pair<LL,PII> >va,vb; PII ans[2]; LL a[2010],b[2010],suma=0,sumb=0,sum=0; LL absl(LL a) { if(a>0)return a; else return -a; } int main() { //FOP; int n,m; Si(n); int i,j; for(i=1; i<=n; i++) { Sl(a[i]); suma+=a[i]; for(j=1;j<i;j++) { va.push_back({a[j]+a[i],{j,i}}); } } Si(m); for(i=1; i<=m; i++) { Sl(b[i]); sumb+=b[i]; for(j=1;j<i;j++) { vb.push_back({b[j]+b[i],{j,i}}); } } LL min=absl(suma-sumb); LL temp; int top=0; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { temp=absl((suma-a[i]+b[j])-(sumb-b[j]+a[i])); if(min>temp) { min=temp; top=1; ans[0]={i,j}; } } } sort(va.begin(),va.end()); sort(vb.begin(),vb.end()); for(i=0,j=0;i<va.size()&&j<vb.size();) { temp=(suma-va[i].first+vb[j].first)-(sumb-vb[j].first+va[i].first); if(min>absl(temp)) { min=absl(temp); top=2; ans[0]={va[i].second.first,vb[j].second.first}; ans[1]={va[i].second.second,vb[j].second.second}; } if(temp>0)i++; else j++; } Pl(min); Pi(top); if(top==1)printf("%d %d\n",ans[0].first,ans[0].second); else if(top==2) { printf("%d %d\n",ans[0].first,ans[0].second); printf("%d %d\n",ans[1].first,ans[1].second); } return 0; }