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. Java、JVM和操作系统之间的关系,写给新人,

    来张图:这个帖子写给新人的,老玩家就直接无视他,因为这个完完全全是白话基础原理. 解释:上面的图是从上往下依次调用的关系. 操作系统(Windows/Linux)管理硬件,让硬件能够正常.合理的运行, ...

  2. 在Ecshop后台打印订单页面将商品按货号排序

    ECSHOP后台管理里的“打印订单" 页面里的商品排序有点乱,现在想改成按序号来排序,修改方法如下 下面是在2.7.2基础上做的修改 打开 admin/order.php  文件 找到(大约 ...

  3. samba服务器源码安装(非rpm)

    首先我们创建一个文档,边安装配置samba,边写教程. 从www.samba.org下载samba最新源码包,我下载的是samba-3.0.7.tar.gz,把它放在我的目录的中/root/lova/ ...

  4. Creating default object from empty value in PHP?

      Your new environment may have E_STRICT warnings enabled in error_reporting if it is PHP <= 5.3, ...

  5. okhttp封装

    对这玩意并不熟,网上有很多大神封装好的,但是懒得看里面的封装逻辑,索性自己简单做个封装,方便使用,出现bug也好查找: get请求: /** * get请求 * @param url * @param ...

  6. 用R分析时间序列(time series)数据

    时间序列(time series)是一系列有序的数据.通常是等时间间隔的采样数据.如果不是等间隔,则一般会标注每个数据点的时间刻度. time series data mining 主要包括decom ...

  7. ubuntu下环境变量

    01:/etc/environment:是设置整个系统的环境: 02:/etc/profile:是设置所有用户的环境: 03:.bashrc :本地用户:

  8. myDatePinker设置开始时间不能大于结束时间

     操作时间起:  <input type="text" name="startTime" id="startTime" style=& ...

  9. WinMain函数详解(转载)

    略加增添与修改! 工具:VC++6.0       系统:win7 64位 在Windows应用程序中,我们可以认为 WinMain() 函数是程序的入口,WinMain()的原型如下: int WI ...

  10. Activity和Service的生命周期(图)

    1.Activity的生命周期 情形一.一个单独的Activity的正常的生命过程是这样的:onCreate->onStart->onPause->onStop->onDest ...