UVa 1152 -4 Values whose Sum is 0—[哈希表实现]
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—[哈希表实现]的更多相关文章
- 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< ...
- UVA - 1152 4 Values whose Sum is 0(中途相遇法)
题意:从四个集合各选一个数,使和等于0,问有多少种选法. 分析:求出来所有ai + bi,在里面找所有等于ci + di的个数. #pragma comment(linker, "/STAC ...
- 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 ...
- UVA - 1152 --- 4 Values whose Sum is 0(二分)
问题分析 首先枚举a和b, 把所有a+b记录下来放在一个有序数组,然后枚举c和d, 在有序数组中查一查-c-d共有多少个.注意这里不可以直接用二分算法的那个模板,因为那个模板只能查找是否有某个数,一旦 ...
- UVA - 1152 4 Values whose Sum is 0问题分解,二分查找
题目:点击打开题目链接 思路:暴力循环显然会超时,根据紫书提示,采取问题分解的方法,分成A+B与C+D,然后采取二分查找,复杂度降为O(n2logn) AC代码: #include <bits/ ...
- UVA 1152 4 Values Whose Sum is Zero 和为0的4个值 (中途相遇)
摘要:中途相遇.对比map,快排+二分查找,Hash效率. n是4000的级别,直接O(n^4)肯定超,所以中途相遇法,O(n^2)的时间枚举其中两个的和,O(n^2)的时间枚举其他两个的和的相反数, ...
- 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 ...
- K - 4 Values whose Sum is 0(中途相遇法)
K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS Memory Limi ...
- POJ 2785 4 Values whose Sum is 0(想法题)
传送门 4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 20334 A ...
随机推荐
- phpstorm配置Xdebug进行调试PHP教程_php技巧_脚本之家
运行环境: PHPSTORM版本 : 8.0.1 PHP版本 : 5.6.2 xdebug版本:php_xdebug-2.2.5-5.6-vc11-x86_64.dll ps : php版本和xdeb ...
- jquery find 推荐
https://codeplayer.vip/p/j7soa 这篇写的还是不错的,备用. // 返回jQuery对象所有匹配元素的标识信息数组 // 每个元素形如:tagName或tagName#id ...
- Ceph 之Multisite 下的bucket reshard
目录 一.背景和问题 二.bucket reshard 过程 主集群信息汇总 Multisite 下手动reshard References 一.背景和问题 默认情况下只有当单个bucket承载的ob ...
- day40-Spring 02-事务的回顾
- js日历
html部分 <div class="div"> <p><span onclick="syn()">上一年</span ...
- 2019-1-29-jekyll-如何加密博客-防止抓取
title author date CreateTime categories jekyll 如何加密博客 防止抓取 lindexi 2019-01-29 16:26:17 +0800 2018-2- ...
- C/S和B/S交互 2016-03-19 11:27 1275人阅读 评论(30) 收藏
最近一直在做C/S的项目,每天都超忙,抽个时间写篇博客,之前一直做C/S项目就是各种窗体,各种控件,拖来拖去,然后点进去写方法,做BS的时候呢,因为一直使用的是mvc,所以就是经常手写代码,或者拖引用 ...
- Myeclipse 方法中文注释看不到
参考以下几种解决方式: 1 改变整个文件类型的编码格式 1) eclipse->window->preferences->General->Content Types 2) 找 ...
- 因为对 Docker 不熟悉建了 N 多个 Nginx
因为对 Docker 不熟悉建了 N 多个 Nginx 一直不停的 docker run nginx 结果出现无数个 nginx. 然后最原来的 nginx 启动不了了. 使用 docker ps - ...
- Python基础:19类和实例的内建函数
1:issubclass() issubclass()布尔函数,判断一个类是否是另一个类的子类或子孙类.它有如下语法:issubclass(sub,sup) 这个函数也允许“不严格”的子类,意味着,一 ...