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 ...
随机推荐
- 用五种不同的布局方式实现“左右300px中间自适应”的效果
float浮动 <section class="layout float"> <style media="screen"> .layou ...
- 如何用好消息推送(push)做APP运营
作为移动端APP产品运营最重要的运营手段,消息推送(push)被越来越多的APP厂商所重视,在信息泛滥的移动互联网时代,手机APP应用安装得越来越多,小小的手机屏幕每天收到的消息推送也越来越多,站在用 ...
- Servlet工作流程
1.加载Servlet类 类加载器负责加载servlet类. 当Web容器接收到servlet的第一个请求时,将加载servlet类. 2.创建Servlet实例 Web容器在加载servlet类之后 ...
- (六)IO流之过滤流
过滤字节流FilterInputStream和FilterOutputStream BufferedInputStream和BufferedOutputStream 需要使用已存在的节点流来构造 ...
- android 重写系统进度条
转载自http://blog.csdn.net/codingandroid/article/details/8495074 自定义progressbar现在要自定义一个等待的时候转动的小圈,相信大家也 ...
- 百度语音识别REST API用法(含JAVA代码)——不须要集成SDK的方法
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zpf8861/article/details/32329457 上一篇文章http://blog.c ...
- 如何处理iOS中照片的方向
使用过iPhone或者iPad的朋友在拍照时不知是否遇到过这样的问题,将设备中的照片导出到Windows上时,经常发现导出的照片方向会有问题,要么横着,要么颠倒着,需要旋转才适合观看.而如果直接在这些 ...
- UVa 10285【搜索】
UVa 10285 哇,竟然没超时!看网上有人说是记忆化搜索,其实不太懂是啥...感觉我写的就是毫无优化的dfs暴力....... 建立一个坐标方向结构体数组,每个节点dfs()往下搜就好了. #in ...
- Java练习 SDUT-1230_平方和与立方和
平方和与立方和 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇 ...
- @bzoj - 4378@ [POI2015] Logistyka
目录 @description@ @solution@ @accepted code@ @details@ @description@ 维护一个长度为 n 的序列,一开始都是 0,支持以下两种操作: ...