您的位置:首页 > Web前端

codeforces_620D. Professor GukiZ and Two Arrays

2016-04-05 20:09 246 查看
D. Professor GukiZ and Two Arraystime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputProfessor GukiZ has two arrays of integers, a and b. Professor wants to make the sum of the elements in the array a sa asclose as possible to the sum of the elements in the array b sb. So he wants to minimizethe 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] andthe array b is [3, 3, 2] professor can swap the element 5 from the array a and theelement 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.InputThe 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.OutputIn the first line print the minimal value v = |sa - sb| thatcan 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.Examplesinput
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
调换次数最大为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 longusing 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].secon4000d);}return 0;}
  
Educational Codeforces Round 6
Finished
Practice
  → Virtual participationVirtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ACM-ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problemsin the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate this other person during a virtual contest.  → Submit?
Language:GNU GCC 5.1.0GNU GCC C11 5.1.0GNU G++ 5.1.0GNU G++11 5.1.0Microsoft Visual C++ 2010C# Mono 3.12.1.0MS C# .NET 4.0.30319D DMD32 v2.069.2Go 1.5.2Haskell GHC 7.8.3Java 1.7.0_80Java 1.8.0_66OCaml 4.02.1Delphi 7Free Pascal 2.6.4Perl 5.20.1PHP 5.4.42Python 2.7.10Python 3.5.1PyPy 2.7.10 (2.6.1)PyPy 3.2.5 (2.4.0)Ruby 2.0.0p645Scala 2.11.7JavaScript V8 4.8.0
Choose file:
  → Last submissions
SubmissionTimeVerdict
17170798Apr/05/2016 14:56Accepted
  → Problem tags    binary search    two pointersNo tag edit access  → Contest materialsAnnouncementTutorial
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: