Tuesday, February 25, 2020

POJ.2785 4 Values whose Sum is 0

1.Problem
http://poj.org/problem?id=2785

2.Idea
Semi-Enumeration

3.Source
 #define MAX_N 4004  
 int n;  
 int A[MAX_N], B[MAX_N], C[MAX_N], D[MAX_N];  
 int CD[MAX_N * MAX_N];  
 void solve()  
 {  
      for (int i = 0; i < n; i++)  
           for (int j = 0; j < n; j++) {  
                CD[i * n + j] = C[i] + D[j];  
           }  
      sort(CD, CD + n*n);  
      ll res = 0;  
      for (int i = 0; i < n; i++)  
           for (int j = 0; j < n; j++) {  
                int cd = -(A[i] + B[j]);  
                res += (upper_bound(CD, CD + n*n, cd) - lower_bound(CD, CD + n*n, cd));  
           }  
      printf("%lld\n", res);  
 }  
 int main()  
 {  
      scanf("%d", &n);  
      for (int i = 0; i < n; i++) {  
           scanf("%d%d%d%d", &A[i], &B[i], &C[i], &D[i]);  
      }  
      solve();  
      return 0;  
 }  

No comments:

Post a Comment