4 Values whose Sum is 0
Time Limit: 15000MS | Memory Limit: 228000K | |
Total Submissions: 29564 | Accepted: 9001 | |
Case Time Limit: 5000MS |
Description
The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute how many quadruplet (a, b, c, d ) ∈ A x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .
Input
The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 228 ) that belong respectively to A, B, C and D .
Output
For each input file, your program has to write the number quadruplets whose sum is zero.
Sample Input
6
-45 22 42 -16 -41 -27
56 30 -36 53 -37 77
-36 30 -75 -46 26 -38
-10 62 -32 -54 -6 45
Sample Output
5
Hint
Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).
给定各有n个整数的四个数列A,B,C,D。要从每个数列中各取出一个数,使四个数的和为0。求出这样的组合的个数。当一个数列中有多个相同的数字时,把它们作为不同的数字看待。
如果直接四层循环做的话复杂度是,显然不可行。
如果将它们对半分成AB和CD再考虑,就可以快速解决了。先从A、B中取出a、b后,为了使总和为0则需要从C、D中取出c+d=-(a+b)。因此先将从C、D中取数字的种组合全部枚举出来,排好序后就能运用二分搜索了。总复杂度为
。
AC代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<sstream> 3 #include<algorithm> 4 #include<string> 5 #include<cstring> 6 #include<iomanip> 7 #include<vector> 8 #include<cmath> 9 #include<ctime> 10 #include<stack> 11 #include<queue> 12 #define e 2.71828182 13 #define Pi 3.141592654 14 using namespace std; 15 int CD[4000*4000]; 16 int main() 17 { 18 int n,A[4000],B[4000],C[4000],D[4000]; 19 cin>>n; 20 for(int i=0;i<n;i++) cin>>A[i]>>B[i]>>C[i]>>D[i]; 21 22 for(int i=0;i<n;i++) 23 for(int j=0;j<n;j++) 24 CD[i*n+j]=C[i]+D[j]; 25 sort(CD,CD+n*n); 26 27 long long ans=0; 28 for(int i=0;i<n;i++) 29 { 30 for(int j=0;j<n;j++) 31 { 32 int AB=-(A[i]+B[j]); 33 //取出C和D中和为AB的部分 34 ans+=upper_bound(CD,CD+n*n,AB)-lower_bound(CD,CD+n*n,AB); 35 } 36 } 37 cout<<ans; 38 }