码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 2141 Can you find it?

时间:2015-08-30 22:42:59      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=2141

Can you find it?

Description

Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.

Input

There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers.

Output

For each case, firstly you have to print the case number as the form "Case d:", then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print "YES", otherwise print "NO".

Sample Input

3 3 3
1 2 3
1 2 3
1 2 3
3
1
4
10

Sample Output

Case 1:
NO
YES
NO

简单的哈希判重。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using std::map;
using std::abs;
using std::find;
using std::pair;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 500007;
const int INF = 0x3f3f3f3f;
struct Hash_Set {
	int tot, num[N], head[N], next[N];
	inline void init() {
		tot = 0, cls(head, -1);
	}
	inline void insert(int val) {
		int u = abs(val) % N;
		num[tot] = val, next[tot] = head[u], head[u] = tot++;
	}
	inline bool find(int val) {
		int u = abs(val) % N;
		for (int i = head[u]; ~i; i = next[i]) {
			if (num[i] == val) return true;
		}
		return false;
	}
}hash;
int A[3][510];
int main() {
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	int l, n, m, t, v, k = 1;
	while (~scanf("%d %d %d", &l, &n, &m)) {
		hash.init();
		rep(i, l) scanf("%d", &A[0][i]);
		rep(i, n) scanf("%d", &A[1][i]);
		rep(i, m) scanf("%d", &A[2][i]);
		rep(i, l) {
			rep(j, n) {
				int val = A[0][i] + A[1][j];
				hash.insert(val);
			}
		}
		scanf("%d", &t);
		printf("Case %d:\n", k++);
		while (t--) {
			bool f = false;
			scanf("%d", &v);
			rep(i, m) {
				if (hash.find(v - A[2][i])) f = true;
			}
			puts(f ? "YES" : "NO");
		}
	}
	return 0;
}

hdu 2141 Can you find it?

标签:

原文地址:http://www.cnblogs.com/GadyPu/p/4771801.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!