题目链接:https://cn.vjudge.net/problem/UVA-1152
题意:
有四个集合,ABCD,分别在这四个集合中放入数字,从四个集合中各取一个数,4个数之和为0的取法有几种;
思路:
最简单想到的就是四重循环,如果有n是4000,那么时间复杂度是64*10^9,1秒大概能运行10^8次运算,显然是超时的;因为要得到的结果是相加之和为0,如果我们知道A+B的值得话,我们只要知道C+D有没有-(A+B)的值就行了,如果不用二分去查找的话,时间复杂度和四重循环是差不多的,但是有了二分就能大大缩短时间;创建A+B的数组,然后依次遍历,每遍历一个数就在C+D的数组中二分查找一次;
第一次WA:我起初是找到有值存在就+1,没有考虑到同一个值,在C+D中可能不止一个,所以+1显然是不够的;
第二次WA:格式错误;在输出ans后面输出一个空格的时候,最后一个案例数不能输出空格,要用if避免多输出一个末尾的空格;
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<sstream>
#include<vector>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0);
typedef long long ll;
const int Maxn = 4010;
const long long LINF = 1e18;
const int INF = 0x3f3f3f3f;
int A[Maxn],B[Maxn],C[Maxn],D[Maxn];
int AB[Maxn*Maxn],CD[Maxn*Maxn];
int main (void)
{
int t,n;
scanf("%d",&t);
for (int cas = 1; cas <= t; ++cas) {
scanf("%d",&n);
for (int i = 1; i <= n; ++i) {
scanf("%d%d%d%d",&A[i],&B[i],&C[i],&D[i]);
}
int M = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
AB[M] = A[i]+B[j];
CD[M] = C[i]+D[j];
M++;
}
}
sort(CD,CD+M);
int ans = 0,tmp,x;
for (int i = 0; i < M; ++i) {
tmp = -AB[i];
x = lower_bound(CD,CD+M,tmp)-CD;
for (int j = x; ; ++j) { // 符合条件的值可能在CD中有多个
if(CD[j] == tmp) ans++;
else break;
}
}
printf("%d\n",ans);
if(cas != t) printf("\n");
}
return 0;
}