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 × B × C × D are such that a + b + c + d = . In the following, we
assume that all lists have the same size n.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases
following, each of them as described below. This line is followed by a blank line, and there is also a
blank line between two consecutive inputs.
The first line of the input file contains the size of the lists n (this value can be as large as ).
We then have n lines containing four integer values (with absolute value as large as ) that belong
respectively to A, B, C and D.
Output
For each test case, your program has to write the number quadruplets whose sum is zero.
The outputs of two consecutive cases will be separated by a blank line.
Sample Input
1 - -
- -
- -
- - -
- -
- - - 45

Sample Output

Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-, -, , ),
(, , -, -), (-, , , -), (-, , -, ), (-, -, , ).

解题思路:

  枚举并存储A+B的和,然后枚举C+D,搜索-C-D的个数,问题的关键是如何存储A+B的和。本题数据量不小,极限数据n=4000时,A+B的和有16,000,000个,数组显然开不下。那么不妨建立哈希表来存储。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <set>
#include <vector>
#include <ctime>
#define H 1000000
#define maxn 4000
#define time__ cout<<" time: "<<double(clock())/CLOCKS_PER_SEC<<endl;
using namespace std;
vector<int> Hash2[H]; int A[maxn+];
int B[maxn+];
int C[maxn+];
int D[maxn+];
int n;
inline void Hash_clear(){
for(int i=;i<H;i++)
Hash2[i].clear();
}
inline int h(int x){
return abs(x%H);
}
inline int count_(int x){ int h_=h(x);
int cnt=;
for(int i=;i<Hash2[h_].size();i++)
if(Hash2[h_][i]==x) cnt++;
return cnt; }
int main(int argc, const char * argv[]) { int T;
scanf("%d",&T);
while (T--) {
Hash_clear(); int cnt=;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d%d%d%d",&A[i],&B[i],&C[i],&D[i]);
for(int i=;i<n;i++)
for(int j=;j<n;j++){
int x=A[i]+B[j]; Hash2[h(x)].push_back(x); }
for(int i=;i<n;i++)
for(int j=;j<n;j++){
int x=C[i]+D[j];
cnt+=count_(-x);
}
cout<<cnt<<endl;
if(T)
cout<<endl;
}
//time__;
return ;
}

UVa 1152 -4 Values whose Sum is 0—[哈希表实现]的更多相关文章

  1. UVA 1152 4 Values whose Sum is 0 (枚举+中途相遇法)(+Java版)(Java手撕快排+二分)

    4 Values whose Sum is 0 题目链接:https://cn.vjudge.net/problem/UVA-1152 ——每天在线,欢迎留言谈论. 题目大意: 给定4个n(1< ...

  2. UVA - 1152 4 Values whose Sum is 0(中途相遇法)

    题意:从四个集合各选一个数,使和等于0,问有多少种选法. 分析:求出来所有ai + bi,在里面找所有等于ci + di的个数. #pragma comment(linker, "/STAC ...

  3. UVa 1152 4 Values whose Sum is 0

    题意:给出n,四个集合a,b,c,d每个集合分别有n个数,分别从a,b,c,d中选取一个数相加,问使得a+b+c+d=0的选法有多少种 看的紫书,先试着用hash写了一下, 是用hash[]记录下来a ...

  4. UVA - 1152 --- 4 Values whose Sum is 0(二分)

    问题分析 首先枚举a和b, 把所有a+b记录下来放在一个有序数组,然后枚举c和d, 在有序数组中查一查-c-d共有多少个.注意这里不可以直接用二分算法的那个模板,因为那个模板只能查找是否有某个数,一旦 ...

  5. UVA - 1152 4 Values whose Sum is 0问题分解,二分查找

    题目:点击打开题目链接 思路:暴力循环显然会超时,根据紫书提示,采取问题分解的方法,分成A+B与C+D,然后采取二分查找,复杂度降为O(n2logn) AC代码: #include <bits/ ...

  6. UVA 1152 4 Values Whose Sum is Zero 和为0的4个值 (中途相遇)

    摘要:中途相遇.对比map,快排+二分查找,Hash效率. n是4000的级别,直接O(n^4)肯定超,所以中途相遇法,O(n^2)的时间枚举其中两个的和,O(n^2)的时间枚举其他两个的和的相反数, ...

  7. uva 1152 4 values whose sum is zero ——yhx

    The SUM problem can be formulated as follows: given four lists A;B;C;D of integer values, computehow ...

  8. K - 4 Values whose Sum is 0(中途相遇法)

    K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS     Memory Limi ...

  9. POJ 2785 4 Values whose Sum is 0(想法题)

    传送门 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 20334   A ...

随机推荐

  1. 【风马一族_SQL Server】

    原文来自:http://www.cnblogs.com/sows/p/6097684.html (博客园的)风马一族 侵犯版本,后果自负 2016-11-24  14:25:45 命令行方式处理服务管 ...

  2. 大数据心法来了!一站式玩转MaxCompute,还有开发者资源等你领!

    阿里云大数据计算平台开发者版2019年3月推出,MaxCompute正在成为开发者的免费大数据平台.今天,MaxCompute在企业构建自己的数据处理平台实践中起到了至关重要的作用,我们特别精选了企业 ...

  3. 【软件安装】python安装numpy、scipy

    python2.7开发环境,若为python3.4的环境则下载对应的软件 系统为64为windows环境,显然不同于32的环境,更繁琐,所谓的网友教程也不尽人意. 安装numpy 下载地址:http: ...

  4. C++复制初始化的限制

    相比于直接初始化,复制初始化有更加严格的限制. 1:在复制初始化时,不能使用声明为explicit的构造函数进行的隐式转换.而直接初始化则是允许的: struct Exp { explicit Exp ...

  5. ES6对象的super关键字

    super是es6新出的关键字,它既可以当作函数使用,也可以当作对象使用,两种使用方法不尽相同 1.super用作函数使用的时候,代表父类的构造函数,es6规定在子类中使用this之前必须先执行一次s ...

  6. Cython保护Python代码

    注:.pyc也有一定的保护性,容易被反编译出源码... 项目发布时,为防止源码泄露,需要对源码进行一定的保护机制,本文使用Cython将.py文件转为.so进行保护.这一方法,虽仍能被反编译,但难度会 ...

  7. AtCoder Beginner Contest 078 C HSI

    虽说这是个水题,但是我做了大概有一个小时吧,才找到规律,刚学概率,还不大会做题. 找到规律后,又想了想,才想到推导过程. 思路:想要知道花费的时间,就要知道提交的次数,我在这里是计算的提交次数的期望, ...

  8. XML内部DTD约束 Day24

    <?xml version="1.0" encoding="UTF-8"?> <!-- 内部DTD --> <!-- XML:ex ...

  9. android学习——android项目的的目录结构

       然后我们看一下Helloword的程序目录: 我们可以看到 大致有的文件: 1. MainHelloWorld.java文件 2. R.java文件 3. android.jar文件 4. RE ...

  10. poj 3743 LL’s cake (PSLG,Accepted)

    3743 -- LL’s cake 搞了好久都过不了,看了下题解是用PSLG来做的.POJ 2164 && LA 3218 Find the Border (Geometry, PSL ...