例题8-3 和为0的4个值(4 Values Whose Sum is Zero, ACM/ICPC SWERC 2005, UVa 1152)

用map效率有点低啊,tle了。
#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define SF(a) scanf("%d", &a)
#define PF(a) printf("%d\n", a)  
#define SFF(a, b) scanf("%d%d", &a, &b)  
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define LL long long
#define maxn 4005
#define maxm 205
#define mod 1000000007
#define INF 10000007
#define eps 1e-4
using namespace std;
//-------------------------CHC------------------------------//
int a[maxn], b[maxn], c[maxn], d[maxn];
int sum[maxn*maxn];

int main() {
	int t;
	SF(t);
	while (t--) {
		int n;
		SF(n);
		FOR(i, 0, n) SFFF(a[i], b[i], c[i]), SF(d[i]);
		int ns = 0;
		FOR(i, 0, n) FOR(j, 0, n) sum[ns++] = a[i] + b[j];
		LL ans = 0;
		sort(sum, sum + ns);
		FOR(i, 0, n) FOR(j, 0, n)
			ans += upper_bound(sum, sum + ns, -c[i] - d[j]) - lower_bound(sum, sum + ns, -c[i] - d[j]);
		printf("%lld\n", ans);
		if (t) puts("");
	}
	return 0;
}

hash实现 1.01s

#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define SF(a) scanf("%d", &a)
#define PF(a) printf("%d\n", a)  
#define SFF(a, b) scanf("%d%d", &a, &b)  
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define LL long long
#define maxn 4005
#define maxm 205
#define mod 1000000007
#define INF 10000007
#define eps 1e-4
using namespace std;
//-------------------------CHC------------------------------//
struct hash_map {
	static const int mask = 0x7ffffff;
	int q[mask + 1], p[mask + 1];
	void clear() { CLEAR(q, 0); }
	int& operator[](int k) {
		int i;
		for (i = k & mask; q[i] && p[i] != k; i = (i + 1) & mask);
		p[i] = k; return q[i];
	}
}Hash;

int a[maxn], b[maxn], c[maxn], d[maxn];

int main() {
	int t;
	SF(t);
	while (t--) {
		Hash.clear();
		int n;
		SF(n);
		FOR(i, 0, n) SFFF(a[i], b[i], c[i]), SF(d[i]);
		FOR(i, 0, n) FOR(j, 0, n) ++Hash[a[i] + b[j]];
		LL ans = 0;
		FOR(i, 0, n) FOR(j, 0, n) ans += Hash[-c[i] - d[j]];
		printf("%lld\n", ans);
		if (t) puts("");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值