zoukankan      html  css  js  c++  java
  • hdu 2141 Can you find it?

    题目连接

    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:
    ", 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;
    }
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    【C++FAQ】如何设定小数点后的显示位数
    【C++FAQ】怎么输入一行字符串(可能带空格)
    c++ operator重载的例子
    【C++FAQ】怎么给结构体排序
    【IT面试题007】英语字符串的分词程序
    【C++/C FAQ】如何格式化输出以0填充的定长整数
    nginx的root和alias指令的区别
    linux磁盘满了,各种奇怪错误
    使用nginx搭建http代理服务器
    nginx图片过滤处理模块http_image_filter_module安装配置笔记
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4771801.html
Copyright © 2011-2022 走看看