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) 收藏的更多相关文章

  1. 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 ...

  2. winform只允许一个应用程序运行 2014-12-08 09:51 31人阅读 评论(0) 收藏

    使用互斥体Mutex类型 导入命名空间 using System.Threading; //声明互斥体 Mutex mutex = new Mutex(false, "ThisShouldO ...

  3. Java内部类总结 分类: 原理 2015-06-28 09:51 9人阅读 评论(0) 收藏

    内部类是指在一个外部类的内部再定义一个类.内部类作为外部类的一个成员,并且依附于外部类而存在的. 内部类可为静态,可用protected和private修饰(而外部类只能使用public和缺省的包访问 ...

  4. windows设置多长时间后自动关机 分类: windows常用小技巧 2014-04-15 09:35 230人阅读 评论(0) 收藏

    分二步: 第一步:点击windows键,在"搜索程序和文件"的文本框输入:cmd 第二步:输入:shutdown -s -t          (设置电脑一小时后自动关机) 备注: ...

  5. 快速查询本机IP 分类: windows常用小技巧 2014-04-15 09:28 138人阅读 评论(0) 收藏

    第一步: 点击windows建(屏幕左下方),在搜索程序和文件文本框内输入:cmd 第二步:      点击Enter建进入. 第三步: 输入:ipconfig即可. 版权声明:本文为博主原创文章,未 ...

  6. sscanf 函数 分类: POJ 2015-08-04 09:19 4人阅读 评论(0) 收藏

    sscanf 其实很强大 分类: 纯C技术 技术笔记 2010-03-05 16:00 12133人阅读 评论(4) 收藏 举报 正则表达式stringbuffercurlgoogle 最近在做日志分 ...

  7. 哈希-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 ...

  8. Ombrophobic Bovines 分类: POJ 图论 最短路 查找 2015-08-10 20:32 2人阅读 评论(0) 收藏

    Ombrophobic Bovines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16539 Accepted: 3605 ...

  9. PIGS 分类: POJ 图论 2015-08-10 09:15 3人阅读 评论(0) 收藏

    PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18209 Accepted: 8277 Description Mir ...

随机推荐

  1. ViewController添加子控制器 并且弹出

    /** *  初始化子控制器 */ - (void)setupChildVcs { for (int i = 0; i<6; i++) { UIViewController *vc = [[UI ...

  2. Lintcode: Count of Smaller Number

    Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...

  3. Leetcode: Alien Dictionary && Summary: Topological Sort

    There is a new alien language which uses the latin alphabet. However, the order among letters are un ...

  4. 二分多重匹配(HDU5093)

    Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tot ...

  5. 部署ganglia3.7

    环境 centOS6.6 gmetad节点关闭iptable gmetad和httpd只需要在一台节点安装,gmond需要在每台节点上安装. 一.安装epel源 sudo wget http://do ...

  6. Kafka集群模式部署

    环境:kafka 0.8.1.1 基本概念 Kafka维护按类区分的消息,称为主题(topic) 生产者(producer)向kafka的主题发布消息 消费者(consumer)向主题注册,并且接收发 ...

  7. explode and implode

    [PHP源码阅读]explode和implode函数   explode和implode函数主要用作字符串和数组间转换的操作,比如获取一段参数后根据某个字符分割字符串,或者将一个数组的结果使用一个字符 ...

  8. paper 20 :color moments

    图像的颜色特征是很重要的,其中颜色矩也是很重要的一部分.(还有一个关于图像颜色特征的review,对于image color写的比较全面).还有,我要强调一下,本blog上的链接都是Google学术上 ...

  9. oracle的簇的创建

    簇其实就是一组表,由一组共享相同数据块的多个表组成,将经常一起使用的表组合在一起成簇可以提高处理效率:在一个簇中的表就叫做簇表. 建立顺序是:簇→簇表→簇索引→数据 创建簇的格式 CREATE CLU ...

  10. Cloudera CDH 、Impala本地通过Parcel安装配置详解

    一.Parcel本地源与Package本地源的区别 本地通过Parcel安装过程与本地通过Package安装过程完全一致,不同的是两者的本地源的配置. 区别如下: Package本地源:软件包是.rp ...