4 Values whose Sum is 0

Time Limit: 15000MS   Memory Limit: 228000K
Total Submissions: 21370   Accepted: 6428
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

// 题意: 比如例子 6 行,每行 4 个数,从第一,二,三,四列各选一个数,要使和为 0 ,有几种组合?

n 最大有4000,但还是可以暴力,加二分

a + b = -( c + d )

枚举 a + b ,对于 c + d ,枚举一次,用一个大数组保存和,sort ,就可以二分了。要注意的是,二分到了要对左右继续搜索一下,不同的位置代表有不同的组合

 #include <iostream>
 #include <stdio.h>
 #include <string.h>
 #include <algorithm>

 using namespace std;

 int n;
 int k;

 ][];
 ];

 int erfen(int t)
 {
     ,r=k-;
     while (l<=r)
     {
         ;
         if (s[mid]==t)
         {
             ;
             ;
             &&s[e]==t)
                 e--,all++;
             e=mid+;
             while (e<k&&s[e]==t)
                 e++,all++;
             return all;
         }
         else if (s[mid]>t)
             r=mid-;
         else
             l=mid+;
     }
     ;
 }

 int main()
 {
     while (scanf("%d",&n)!=EOF)
     {
         ;i<n;i++)
             scanf(],&num[i][],&num[i][],&num[i][]);
         k=;
         ;i<n;i++)
             ;j<n;j++)
             s[k++]=-(num[i][]+num[j][]);
         sort(s,s+k);
         ;
         ;i<n;i++)
         {
             ;j<n;j++)
             {
                 ]+num[j][];
                 total+=erfen(left);
             }
         }
         printf("%d\n",total);
     }
     ;
 }

4 Values whose Sum is 0(二分)的更多相关文章

  1. POJ - 2785 4 Values whose Sum is 0 二分

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

  2. POJ - 2785 - 4 Values whose Sum is 0 - 二分折半查找

    2017-08-01 21:29:14 writer:pprp 参考:http://blog.csdn.net/piaocoder/article/details/45584763 算法分析:直接暴力 ...

  3. [poj2785]4 Values whose Sum is 0(hash或二分)

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

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

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

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

  6. UVA1152-4 Values whose Sum is 0(分块)

    Problem UVA1152-4 Values whose Sum is 0 Accept: 794  Submit: 10087Time Limit: 9000 mSec Problem Desc ...

  7. POJ:2785-4 Values whose Sum is 0(双向搜索)

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

  8. 4 Values whose Sum is 0 POJ - 2785

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

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

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

随机推荐

  1. WPF-非矩形窗口的创建

    第一.窗口的AllowsTransparency设置为True 第二.窗口的Background设置为Transparent 第三.窗口的WindowStyle设置为None 第四.窗口内的Grid用 ...

  2. Java存储密码用字符数组

    字符数组和字符串都可以用于存储文本数据,但是在选择具体哪一种时,如果你没有针对具体的情况是很难回答这个问题的.但是任何与字符串相关的问题一定有线索可以在字符串的属性里面找到,比如不可变性.他就用这种方 ...

  3. 用css控制cellspacing、cellpadding

    用css控制cellspacing.cellpadding 发表于 2006 年 12 月 24 日 由 Silbo 在table中控制单元格之间的间距要用到cellspacing.cellpaddi ...

  4. win7开机密码忘记了

    开机到欢迎界面时,出现输入用户名密码的提示框,按Ctrl+Alt+Delete,跳出帐号窗口,输入用户名:administrator,回车即可. 如果这个administrator帐号也有密码,那么可 ...

  5. 在ubantu上安装hive

    上次我们在ubantu上安装了hadoop,那我们现在再进一步,开始我们的学习之旅--hive板块吧! 第一步:准备! 软件: I.一个搭建好的hadoop环境 II.hive的bin文件(前往apa ...

  6. TP5.0实现无限极回复功能

    最近做项目的时候用到了评论回复,使用ThinkPHP5.0框架做回复碰到了一些问题,简单总结一下.(李昌辉) 1.首先是数据表的设计: create table zy_huifu ( code int ...

  7. Vue的新启之笔

    之前就有接触Vue这一语言,作为一个摊薄饼的我,觉得其基础性的知识体系与其他语言是相通的.且由于贵阳这一城市的地理位置的特殊性,我不得不承认想要从事软件开发这一行业,不精通一门语言不行.因为,任何一家 ...

  8. cmake常用编译选项

    1.openmp加载,cmake3.9之后内置了openmp find_package(OpenMP) if (OPENMP_FOUND OR OpenMP_CXX_FOUND) set (CMAKE ...

  9. About Apache Cordova

    Apache Cordova is a set of device APIs that allow a mobile app developer to access native device fun ...

  10. 深入研究嵌入式web服务器的视频监控应用

    http://embed.chinaitlab.com/pc/776136.html uCLinux下,主要有3个Web Server:Httpd.Thttpd和BOA.Httpd是最简单的一个Web ...