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. pytest fixture 利用 params参数实现用例集合

    @pytest.fixture有一个params参数,接受一个列表,列表中每个数据都可以作为用例的输入.也就说有多少数据,就会形成多少用例.如下面例子,就形成3条用例 test_parametrizi ...

  2. 大合集 | 9 场 Dubbo Meetup 回顾视频和PPT下载

    2018年2月16日,Dubbo 加入 Apache 基金会孵化器,在随后的那个夏天,我们开始在全国(北京.上海.深圳.成都.杭州)和社区的用户们见面.认识.交个朋友,将社区的最新动态.Dubbo 的 ...

  3. nth-child和nth-of-type的使用案列

    HTML: <div id="footer-f"> <ul class="trajectory"> <li> <div ...

  4. Directx教程(29) 简单的光照模型(8)

    原文:Directx教程(29) 简单的光照模型(8)      现在我们新建一个工程myTutorialD3D_23,在这个工程中,对前面一章的代码进行一些整理: 1.我们在顶点属性中增加材质的的漫 ...

  5. WPF/Silverlight深度解决方案:(六)HLSL自定义渲染特效之完美攻略(上)

    原文:WPF/Silverlight深度解决方案:(六)HLSL自定义渲染特效之完美攻略(上) Shader Effect种位图特效及2种渲染特效,而Silverlight中仅有这2种渲染特效: Bl ...

  6. 获取登录的地点和ip地址的js

    <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script> <script>doc ...

  7. LeetCode108 Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. (M ...

  8. Myeclipse 设置默认注释

    windows-->preference-->Java-->Code Style-->Code Templates code-->New Java files ${fil ...

  9. @atcoder - AGC034E@ Complete Compress

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 N 个点的树,编号为 1, 2, ..., N.第 i ...

  10. Navicat连接MySQL8.0版本时 建议升级连接客户端这个提示怎么办

    开始->mysql 8.0 command line client ->执行下面的命令//开启mysql服务mysql.server start//进入mysqlmysql -u root ...