题干:

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 2 28 ) 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).

解题报告:

   这题要求和为0,显然直接暴力是不存在的,但是还是有解题线索的,比如说一共就四个数,显然可以折半啊!!相同思路的遇到过一次了吧?题目名叫“暴力AC”,写过那篇博客但是具体哪场比赛忘记了。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll a[4005],b[4005],c[4005],d[4005];
int tot1=0,tot2=0;
ll ans1[16000000],ans2[16000000];
int get(ll x) {
	return lower_bound(ans2+1,ans2+tot2+1,x) - ans2;
}
int main()
{
	int n;
	while(cin>>n) {
		for(int i = 1; i<=n; i++) {
			scanf("%lld%lld%lld%lld",a+i,b+i,c+i,d+i);
		}
		tot1=tot2=0;
		for(int i = 1; i<=n; i++) {
			for(int j = 1; j<=n; j++) {
				ans1[++tot1] = a[i] + b[j];
				ans2[++tot2] = c[i] + d[j];
			}
		}
		ll ans = 0;
	//	sort(ans1+1,ans1+tot1+1);
		sort(ans2+1,ans2+tot2+1);
		
		for(int i = 1; i<=tot1; i++) {
			if(binary_search(ans2+1,ans2+tot2+1,-ans1[i]))
				ans += get(-ans1[i]+1) - get(-ans1[i]);
		}
		printf("%lld\n",ans);	
	}

	return 0 ;
 }

总结:

  本来想着可能会炸空间,是不是需要离散化一下,但是后来一想发现不可以这样,相反,如果离散化了这题就错了,因为他要计算的是次数,所以不能去重啊!!而且枚举的时候不能直接加binarysearch,而是应该先判断是否存在,如果存在的话加上所有存在的个数,可以按照我这样写,也可以一个lowerbound一个upperbound、。、、、

事实证明  longlong的1e7是开的下的,这次提交一共用了97796kB内存而已、、、。