哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏
4 Values whose Sum is 0
Time Limit: 15000MS Memory Limit: 228000K
Total Submissions: 17875 Accepted: 5255
Case Time Limit: 5000MS
Description
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 x B x C x D are such that a + b + c + d = 0 . In the following, we assume that all lists have the same size n .
Input
The first line of the input file contains the size of the lists n (this value can be as large as 4000). We then have n lines containing four integer values (with absolute value as large as 228 ) that belong respectively to A, B, C and D .
Output
For each input file, your program has to write the number quadruplets whose sum is zero.
Sample Input
6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45
Sample Output
5
Hint
Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).
Source
Southwestern Europe 2005
哈希的一道比较简单的题,
题意:给你四个集合,从四个集合中分别选出一个元素,使四个元素的和为零,问有几种选法;
方法:先让两个集合加和,用哈希链表,储存计算的结果,有后两个集合的计算结果查找,不过直接写链表可能比较耗时,我交了一次11s多,后来换成前向星3s多.
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-9
#define LL long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define RR freopen("input.txt","r",stdin)
#define WW freopen("output.txt","w",stdout)
const int MAX = 4010;
const int Mod = 10000007;
struct node
{
int num;
int data;
int next;
} H[Mod*10];
int head[Mod];
int a[MAX],b[MAX],c[MAX],d[MAX];
int top;
int Creat(int ans)
{
H[top].num=1;
H[top].data=ans;
return top++;
}
int main()
{
int n;
int ans;
int p,q;
int Max;
while(~scanf("%d",&n))
{
memset(head,-1,sizeof(head));
for(int i=0; i<n; i++)
{
scanf("%d %d %d %d",&a[i],&b[i],&c[i],&d[i]);
}
top=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
ans=a[i]+b[j];
p=abs(ans)%Mod;
q=head[p];
while(q!=-1)
{
if(H[q].data==ans)
{
H[q].num++;
break;
}
q=H[q].next;
}
if(q==-1)
{
q=Creat(ans);
H[q].next=head[p];
head[p]=q;
}
}
}
Max=0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
ans=-1*(c[i]+d[j]);
p=abs(ans)%Mod;
q=head[p];
while(q!=-1)
{
if(H[q].data==ans)
{
Max+=H[q].num;
break;
}
q=H[q].next;
}
}
}
printf("%d\n",Max);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
哈希-4 Values whose Sum is 0 分类: POJ 哈希 2015-08-07 09:51 3人阅读 评论(0) 收藏的更多相关文章
- Max Sum—hdu1003(简单DP) 标签: dp 2016-05-05 20:51 92人阅读 评论(0)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- winform只允许一个应用程序运行 2014-12-08 09:51 31人阅读 评论(0) 收藏
使用互斥体Mutex类型 导入命名空间 using System.Threading; //声明互斥体 Mutex mutex = new Mutex(false, "ThisShouldO ...
- Java内部类总结 分类: 原理 2015-06-28 09:51 9人阅读 评论(0) 收藏
内部类是指在一个外部类的内部再定义一个类.内部类作为外部类的一个成员,并且依附于外部类而存在的. 内部类可为静态,可用protected和private修饰(而外部类只能使用public和缺省的包访问 ...
- windows设置多长时间后自动关机 分类: windows常用小技巧 2014-04-15 09:35 230人阅读 评论(0) 收藏
分二步: 第一步:点击windows键,在"搜索程序和文件"的文本框输入:cmd 第二步:输入:shutdown -s -t (设置电脑一小时后自动关机) 备注: ...
- 快速查询本机IP 分类: windows常用小技巧 2014-04-15 09:28 138人阅读 评论(0) 收藏
第一步: 点击windows建(屏幕左下方),在搜索程序和文件文本框内输入:cmd 第二步: 点击Enter建进入. 第三步: 输入:ipconfig即可. 版权声明:本文为博主原创文章,未 ...
- sscanf 函数 分类: POJ 2015-08-04 09:19 4人阅读 评论(0) 收藏
sscanf 其实很强大 分类: 纯C技术 技术笔记 2010-03-05 16:00 12133人阅读 评论(4) 收藏 举报 正则表达式stringbuffercurlgoogle 最近在做日志分 ...
- 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13215 Accepted: 3873 ...
- Ombrophobic Bovines 分类: POJ 图论 最短路 查找 2015-08-10 20:32 2人阅读 评论(0) 收藏
Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16539 Accepted: 3605 ...
- PIGS 分类: POJ 图论 2015-08-10 09:15 3人阅读 评论(0) 收藏
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18209 Accepted: 8277 Description Mir ...
随机推荐
- Java NIO 读数据处理过程
这两天仿hadoop 写java RPC框架,使用PB作为序列号工具,在写读数据的时候遇到一个小坑.之前写过NIO代码,恰好是错误的代码产生正确的逻辑,误以为自己写对了.现在简单整理一下. 使用NIO ...
- C# 以管理员方式启动Winform,进而使用管理员控制Windows Service
问题起因: 1,) 问题自动分析Windows服务在正常运行时,确实会存在程序及人为原因导致该服务停止.为了在应用程序使用时确保该服务正常运行,于是有了该讨论主题. 2,)一般账户(尽管是管理员组账户 ...
- SQLSERVER:sqlserver2008r2安装好后,自动提示功能不可以使用
刚安装好的sqlserver2008r2x64,写一些sql时,自动提示功能失效了. 解决排查一: 找到tools->options->Text Editor->Transact-S ...
- PostgreSQL Replication之第十一章 使用Skytools(5)
11.5 关于walmgr 的介绍 walmgr 是一个简化基于文件事务日志传输的工具.早在过去的一些日子里(在9.0版本之前),使用walmgr来简化基本备份是很常见的.随着流复制的引入,情况有了一 ...
- java-语句
JAVA语句 1.顺序语句(用:结束)(一个分号也是一个语句)(多条语句形成符合语句) 2.分支语句(又称条件语句) 1. if 语句 例: int a=10 if(a>0) {System ...
- java-语法
JAVA语法 1.标识符 1.定义:对各种变量.方法.类等进行命名的字符序列 2.规则:他的组成由字母.数字.$,数字不能出现在开始,不能和关键字重复,区分大小写 2.数据类型 1.分类 1基本数据类 ...
- 20145207 《Java程序设计》第8周学习总结
前言: 这两天电路焊小车,有意思归有意思,确实挺忙的.博客到现在才写.执勤看的东西忘得好快呀,莫名的记不住.不说废话了,开始. 教材学习内容总结: 一.NIO和NIO2 1.NIO的定义 InputS ...
- C#.Net GC(garbage Collector) 垃圾回收器
以前一直以为gc的原理很简单,也就是分代处理堆数据,直到我的膝盖中了一箭(好吧 直到有天汪涛和我说他面试携程的面试题 关于服务器和 工作站gc 的区别)其实我当时尚不知道 工作站和服务器有什么区别更不 ...
- 1. 星际争霸之php面向对象(一)
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- Hadoop之TaskAttemptContext类和TaskAttemptID类
先来看看TaskAttemptContext的类图 : Figure1:TaskAttemptContext类图 用户向Hadoop提交Job(作业),Job在JobTracker对象的控制下执行.J ...