13773503 | njczy2010 | 2785 | Accepted | 25248K | 7079MS | G++ | 1423B | 2015-01-11 10:26:48 |
4 Values whose Sum is 0
Time Limit: 15000MS | Memory Limit: 228000K | |
Total Submissions: 16102 | Accepted: 4659 | |
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).
Source
sign,抱着脑袋想了好久怎么用hash做,就是没想着用二分,,,,,,
1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<map> 9 #include<set> 10 #include<stack> 11 #include<string> 12 13 #define N 4002 14 #define M 10 15 #define mod 1000000007 16 //#define p 10000007 17 #define mod2 1000000000 18 #define ll long long 19 #define LL long long 20 #define eps 1e-9 21 #define inf 0x3fffffff 22 #define maxi(a,b) (a)>(b)? (a) : (b) 23 #define mini(a,b) (a)<(b)? (a) : (b) 24 25 using namespace std; 26 27 int x[N*N]; 28 int a[N],b[N],c[N],d[N]; 29 ll ans; 30 int k; 31 int n; 32 33 void ini() 34 { 35 int i,j; 36 ans=0; 37 for(i=1;i<=n;i++){ 38 scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]); 39 } 40 k=0; 41 for(i=1;i<=n;i++){ 42 for(j=1;j<=n;j++){ 43 x[k]=a[i]+b[j];k++; 44 } 45 } 46 sort(x,x+k); 47 } 48 49 void solve() 50 { 51 int i,j; 52 int te; 53 int t1,t2; 54 for(i=1;i<=n;i++){ 55 for(j=1;j<=n;j++){ 56 te=-c[i]-d[j]; 57 t1=lower_bound(x,x+k,te)-x; 58 t2=upper_bound(x,x+k,te)-x; 59 ans+=(ll)(t2-t1); 60 } 61 } 62 } 63 64 void out() 65 { 66 printf("%I64d\n",ans); 67 } 68 69 int main() 70 { 71 //freopen("data.in","r",stdin); 72 // freopen("data.out","w",stdout); 73 //scanf("%d",&T); 74 //for(int ccnt=1;ccnt<=T;ccnt++) 75 //while(T--) 76 while(scanf("%d",&n)!=EOF) 77 { 78 ini(); 79 solve(); 80 out(); 81 } 82 83 return 0; 84 }