4 Values whose Sum is 0
Time Limit: 15000MS Memory Limit: 228000K
Total Submissions: 19322 Accepted: 5778
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).

(是不是先得翻译 )题意大概是给定四个长度为n的数组,求四个数组各取一个元素和为0的取法有几种(考虑顺序)

首先有两种做法,一种二分,一种哈希

看一眼数据规模知道枚举每一位的O(n^4)绝对超时

所以观察题目,发现只要求和为0,那么考虑只枚举出前两个数组的任意元素和与后两个数组任意元素和

这样再枚举一遍前两个数组的任意元素和,检查是否有对应元素和为0即可

接下来还要再优化

二分写得很简单,具体代码在《挑战程序设计竞赛》第23页不想写

再来看哈希

把两数组的元素和得出一个键值(就是哈希值)接下来链式储存进去

什么叫链式呢?就是和邻接表差不多

把哈希映射的数组当成head数组,把原值当成边,存原值和next也就是哈希值相同的下一个值的为止

注意这道题还得再优化,多存一个同一数值的重复次数而不是分开存,不然会MLE没错这是省空间用的

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 const int mod=;
 5 typedef struct{
 6         int val;
 7         int num;
 8         int next;
 9 }node;
 int data[][];
 int n,tot=;
 int hash[mod+];
 node all[];
 int abs(int num){
     return num>?num:(-)*num;//考虑负数!考虑负数!考虑负数!
 }
 int get(int num){
     return (abs(num))%mod;
 }
 int add(int num){
     int tmp=get(num);
     int p=;
     if(hash[tmp]){
        for(p=hash[tmp];p!=;p=all[p].next){
            if(all[p].val==num){
               all[p].num++;
               break;
            }
        }
     }
     if((!hash[tmp])||(p==)){
           all[++tot].val=num;
           all[tot].num=;
           all[tot].next=hash[tmp];
           hash[tmp]=tot;
     }
     return ;
 }
 int find(int num){
     int tmp=get(num);
     int p;
     for(p=hash[tmp];p;p=all[p].next){
         if(all[p].val==num)return all[p].num;
     }
     return ;
 }
 int main(){
     memset(hash,,sizeof(hash));
     memset(all,,sizeof(all));
     int n;
     scanf("%d",&n);
     for(int i=;i<=n;i++)scanf("%d %d %d %d",&data[i][],&data[i][],&data[i][],&data[i][]);
     for(int i=;i<=n;i++)for(int j=;j<=n;j++)add(data[i][]+data[j][]);
     int ans=;
     for(int i=;i<=n;i++)for(int j=;j<=n;j++)ans+=find(-(data[i][]+data[j][]));
     printf("%d",ans);
     return ;

58 }

[poj2785]4 Values whose Sum is 0(hash或二分)的更多相关文章

  1. POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)

    题目链接:http://poj.org/problem?id=2785 题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数. 其中一个数列有多个相同的数字时 ...

  2. POJ-2785 Values whose Sum is 0 Hash表

    题目链接:https://cn.vjudge.net/problem/POJ-2785 题意 给出四组数,每组有n个数 现从每组数中取一个数作为a,b,c,d 问有几组这样的a+b+c+d=0 思路 ...

  3. POJ 2785 4 Values whose Sum is 0 Hash!

    http://poj.org/problem?id=2785 题目大意: 给你四个数组a,b,c,d求满足a+b+c+d=0的个数 其中a,b,c,d可能高达2^28 思路: 嗯,没错,和上次的 HD ...

  4. POJ2785 4 Values whose Sum is 0 (二分)

    题意:给你四组长度为\(n\)序列,从每个序列中选一个数出来,使得四个数字之和等于\(0\),问由多少种组成情况(仅于元素的所在位置有关). 题解:\(n\)最大可以取4000,直接暴力肯定是不行的, ...

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

  6. POJ 2785 4 Values whose Sum is 0

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 13069   Accep ...

  7. 折半枚举(双向搜索)poj27854 Values whose Sum is 0

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 23757   Accep ...

  8. POJ 2785 4 Values whose Sum is 0(想法题)

    传送门 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 20334   A ...

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

随机推荐

  1. android:layout_gravity和android:gravity属性的区别

    一.介绍: gravity的中文意思就是”重心“,就是表示view横向和纵向的停靠位置 (1).android:gravity:是对view控件本身来说的,是用来设置view本身的内容应该显示在vie ...

  2. Windows Thin PC 激活方法

    Windows Thin PC 激活方法 笔者之前分享了Windows Thin PC ,如果你已经安装了Windows Thin PC ,但还没有激活,可以参照以下方式进行Windows Thin ...

  3. PHP最原始的上传文件函数

    <?php $upload_file=$_FILES['upload_file']['tmp_name']; $upload_file_name=$_FILES['upload_file'][' ...

  4. cocospod 安装和使用

    一 ruby 安装 要安装coocspod 首先需要安装ruby,可以先安装xcode,在安装macport 下载地址,最后执行命令 port install ruby 二.安装CocoaPods 1 ...

  5. C#中的属性————只谈属性

    废话少说直接一剑封喉--属性是对私有字段的保护(其实是对私有字段引用的另外一种变相公开化),属性在没有任何操作的时候是无法看出其优势来,上例子 // Field used by property.pr ...

  6. MVP+RXJAVA+RecyclerView实现sd卡根目录下的所有文件中的照片加载并显示

    初学Rxjava,目前只能遍历加载指定目录下的所有文件夹中的照片,文件夹中如果还嵌套有文件夹目前还没找到实现方法. 先看mvp目录结构: 很抱歉,没有model. 接下来是view层的接口代码和pre ...

  7. android 6.0权限处理

    在模拟器测试好的程序,运行在mate8上面一直崩,经多方查探才找到以下博文,方法还没掌握,但也算是找到原因了: http://***/article/android-6-0-runtime-permi ...

  8. [Unity3D][Vuforia][IOS]vuforia在unity3d中添加自己的动态模型,识别自己的图片,添加GUI,播放视频

    使用环境 unity3D 5 pro vuforia 4 ios 8.1(6.1) xcode 6.1(6.2) 1.新建unity3d工程,添加vuforia 4.0的工程包 Hierarchy中 ...

  9. 【转】Cookie和Session的区别详解

    转载地址:http://www.phperzone.cn/portal.php?aid=541&mod=view 一.cookie机制和session机制的区别 具体来说cookie机制采用的 ...

  10. jar的下载地址及其使用说明

    有时候会苦于jar的搜索.这里我就给出我平时用到的吧,方便大家.后期会不断添加. 1.dom4j-1.6.1.jar 主要用于解析xml的jar包.下载地址:   http://pan.baidu.c ...