Topic Links:
http://poj.org/problem?id=2785
Test instructions
Given your four-size set of N and then how many four-tuple and 0, respectively, from these four sets;
Analysis:
Divided into two groups, get sum1[[,sum2[], and then order, in the sum2 binary search-sum1[i] write down the number can be
The code is as follows:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm>using namespace std;const int maxn = 4010;int a[4][maxn];int sum1[maxn*maxn],sum2[maxn*maxn];int main () { int n; while (~SCANF ("%d", &n)) {for (int. i=0;i<n;i++) {for (int j=0;j<4;j++) scanf ("%d", &a[j][i] ); } int cnt1=0,cnt2=0; for (int i=0;i<n;i++) {for (int j=0;j<n;j++) sum1[cnt1++]=a[0][i]+a[1][j]; } for (int i=0;i<n;i++) {for (int j=0;j<n;j++) sum2[cnt2++]=a[2][i]+a[3][j]; } Sort (sum1,sum1+cnt1); Sort (sum2,sum2+cnt2); A long long ans = 0; for (int i=0;i<cnt1;i++) ans+=upper_bound (Sum2,sum2+cnt2,0-sum1[i])-lower_bound (Sum2,sum2+cnt2,0-sum1[i] ); printf ("%lld\n", ans); } return 0;}
POJ2785 4 Values whose Sum is 0 (two minutes)