CodeForces 620D Professor GukiZ and Two Arrays
2016-02-14 21:46
816 查看
Professor GukiZ andTwo Arrays
Time Limit:3000MS MemoryLimit:262144KB 64bit IO Format:%I64d& %I64u
Description
Professor GukiZhas two arrays of integers, a and b. Professor wants to make thesum of the elements in the array asa as
closeas possible to the sum of the elements in the array bsb. So he wants tominimize the value v = |sa - sb|.
In one operationprofessor can swap some element from the array a and some elementfrom the array b. For example if the array a is [5, 1, 3, 2, 4] and
thearray b is [3, 3, 2] professorcan swap the element 5 from thearray a and the element 2 from
thearray b and get the new array a[2, 1, 3, 2, 4] and thenew array b[3, 3, 5].
Professordoesn't want to make more than two swaps. Find the minimal value v and somesequence of no
more than two swaps that will lead to the such value v. Professormakes swaps one by one, each new swap he makes with the new arrays a and b.
Input
The first linecontains integer n (1 ≤ n ≤ 2000)
— the numberof elements in the array a.
The second linecontains n integers ai ( - 109 ≤ ai ≤ 109)
— the elementsof the array a.
The third linecontains integer m (1 ≤ m ≤ 2000)
— the numberof elements in the array b.
The fourth linecontains m integers bj ( - 109 ≤ bj ≤ 109)
— the elementsof the array b.
Output
In the firstline print the minimal value v = |sa - sb| that
canbe got with no more than two swaps.
The second lineshould contain the number of swaps k (0 ≤ k ≤ 2).
Each of thenext k linesshould contain two integers xp, yp (1 ≤ xp ≤ n, 1 ≤ yp ≤ m)
— the index ofthe element in the array a and the index of the element in thearray b in the p-th swap.
If there areseveral optimal solutions print any of them. Print the swaps in order theprofessor did them.
Sample Input
Input
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
题目不难,但是做了好多次,码力不够啊。
对于k=0或1时,直接暴力枚举。
k=2时,ans=min(abs(Suma-Sumb-2*(a[i]+a[j])+2*(b[k]+b[l])));
如果暴力枚举每一种需要O(n^4),可以将b枚举出M*(M-1)的情况,再枚举a,对前面的情况进行二分,设T=Suma-Sumb-2*(a[i]+a[j])
要最小化ans,需找到比-T大的第一个数和比-T小的第一个数。
代码如下:
Time Limit:3000MS MemoryLimit:262144KB 64bit IO Format:%I64d& %I64u
Description
Professor GukiZhas two arrays of integers, a and b. Professor wants to make thesum of the elements in the array asa as
closeas possible to the sum of the elements in the array bsb. So he wants tominimize the value v = |sa - sb|.
In one operationprofessor can swap some element from the array a and some elementfrom the array b. For example if the array a is [5, 1, 3, 2, 4] and
thearray b is [3, 3, 2] professorcan swap the element 5 from thearray a and the element 2 from
thearray b and get the new array a[2, 1, 3, 2, 4] and thenew array b[3, 3, 5].
Professordoesn't want to make more than two swaps. Find the minimal value v and somesequence of no
more than two swaps that will lead to the such value v. Professormakes swaps one by one, each new swap he makes with the new arrays a and b.
Input
The first linecontains integer n (1 ≤ n ≤ 2000)
— the numberof elements in the array a.
The second linecontains n integers ai ( - 109 ≤ ai ≤ 109)
— the elementsof the array a.
The third linecontains integer m (1 ≤ m ≤ 2000)
— the numberof elements in the array b.
The fourth linecontains m integers bj ( - 109 ≤ bj ≤ 109)
— the elementsof the array b.
Output
In the firstline print the minimal value v = |sa - sb| that
canbe got with no more than two swaps.
The second lineshould contain the number of swaps k (0 ≤ k ≤ 2).
Each of thenext k linesshould contain two integers xp, yp (1 ≤ xp ≤ n, 1 ≤ yp ≤ m)
— the index ofthe element in the array a and the index of the element in thearray b in the p-th swap.
If there areseveral optimal solutions print any of them. Print the swaps in order theprofessor did them.
Sample Input
Input
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
题目不难,但是做了好多次,码力不够啊。
对于k=0或1时,直接暴力枚举。
k=2时,ans=min(abs(Suma-Sumb-2*(a[i]+a[j])+2*(b[k]+b[l])));
如果暴力枚举每一种需要O(n^4),可以将b枚举出M*(M-1)的情况,再枚举a,对前面的情况进行二分,设T=Suma-Sumb-2*(a[i]+a[j])
要最小化ans,需找到比-T大的第一个数和比-T小的第一个数。
代码如下:
<pre name="code" class="cpp">#include<cstdio> #include<algorithm> #include<cstring> using namespace std; struct num { long long s; int no; }; struct num2 { long long s; int no1; int no2; }; num a[2050]; num b[2050]; num2 d[2050*2050]; int N,M; long long Suma=0,Sumb=0; long long ans; int tot=0; int xa[3]; int xb[3]; long long ABS(long long x) { return x>=0?x:-x; } bool cmp(const num2 x,const num2 y) { return x.s<y.s; } void swap(num &x,num &y) { num z; z=x; x=y; y=z; } void getans1() { long long now=0; for (int i=1;i<=N;i++) { for (int j=1;j<=M;j++) { now=ABS(Suma-Sumb-2*a[i].s+2*b[j].s); if (now<ans) { ans=now; tot=1; xa[1]=i; xb[1]=j; } } } } void getans2() { long long T; int l,r,mid; long long now=0; int NM=0; for (int i=1;i<=M;i++) { for (int j=1;j<=M;j++) { if (i!=j) { NM++; d[NM].s=b[i].s+b[j].s; d[NM].no1=i; d[NM].no2=j; } } } sort(d+1,d+NM+1,cmp); for (int i=1;i<=N;i++) { for (int j=1;j<=N;j++) { if (i==j) { continue; } T=Suma-Sumb-2*a[i].s-2*a[j].s; T=-T; l=1; r=NM; while (r-l>1) { mid=(l+r)>>1; if (d[mid].s*2>T) { r=mid; } else { l=mid; } } l=r-1; if (r<=NM) { now=ABS(T-2*d[r].s); if (now<ans) { ans=now; tot=2; xa[1]=i; xa[2]=j; xb[1]=d[r].no1; xb[2]=d[r].no2; } } if (l>=1) { now=ABS(T-2*d[l].s); if (now<ans) { ans=now; tot=2; xa[1]=i; xa[2]=j; xb[1]=d[l].no1; xb[2]=d[l].no2; } } } } } int main() { scanf("%d",&N); for (int i=1;i<=N;i++) { scanf("%I64d",&a[i].s); a[i].no=i; Suma+=a[i].s; } scanf("%d",&M); for (int i=1;i<=M;i++) { scanf("%I64d",&b[i].s); b[i].no=i; Sumb+=b[i].s; } ans=ABS(Suma-Sumb); tot=0; if (ans) { getans1(); } if (N>=2 && M>=2 && ans) { getans2(); } printf("%I64d\n",ans); printf("%d\n",tot); for (int i=1;i<=tot;i++) { printf("%d %d\n",xa[i],xb[i]); } return 0; }
相关文章推荐
- 快速排序里的学问:从猜数字开始
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- Search Insert Position,Search for a Range,Pow(x, n),Sqrt(x)
- Find Minimum in Rotated Sorted Array II
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- [LeetCode] Sqrt(x)
- [LeetCode] Pow(x, n)
- [LeetCode] Search Insert Position
- [LeetCode] Search for a Range
- [LeetCode] Search in Rotated Sorted Array
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces Round332 部分题解