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 2 28 ) 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^4,肯定不行,可以两两合并,复杂度就是N^2,代码如下:
vector<int> buf[];
vector<long long>sum1,sum2; int main() {
int N,sum = ;
scanf("%d",&N);
for(int i = ; i < N; ++i) { // read
for(int j = ; j < ; ++j) {
int tmp;
scanf("%d",&tmp);
buf[j].push_back(tmp);
}
}
for(int i = ; i < N; ++i) {
for(int j = ; j < N; ++j) {
sum1.push_back(buf[][i] + buf[][j]);
sum2.push_back(buf[][i] + buf[][j]);
}
}
sort(sum1.begin(), sum1.end());
sort(sum2.begin(), sum2.end()); for(int i = ;i < sum1.size(); ++i) {
long long tmp = -sum1[i];
sum += upper_bound(sum2.begin(), sum2.end(), tmp) - lower_bound(sum2.begin(), sum2.end(), tmp);
}
printf("%d",sum);
return ;
}

Day3-J-4 Values whose Sum is 0 POJ2785的更多相关文章

  1. 4 Values whose Sum is 0 [POJ2785] [折半搜索]

    题意 给你长度为n四个数列,每个数列选一个数使总和为4,有多少种选法(不同选法仅当起码有一个元素的下标不同) 输入 第一行,n 下面n行,每行四个数,代表ai,bi,ci,di 输出 选法数量 样例输 ...

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

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

  3. POJ 2785 4 Values whose Sum is 0

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

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

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

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

  6. K - 4 Values whose Sum is 0(中途相遇法)

    K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS     Memory Limi ...

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

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

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

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

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

随机推荐

  1. 服务器(1)——IIS(1)——Windows7中IIS简单安装与配置(详细图解)

    最近工作需要IIS,自己的电脑又是Windows7系统,找了下安装的方法,已经安装成功. 一.首先是安装IIS.打开控制面板,找到“程序与功能”,点进去 二.点击左侧“打开或关闭Windows功能” ...

  2. stackADT

    stack.h #ifndef STACK_H_INCLUDED #define STACK_H_INCLUDED #include <stdbool.h> typedef struct ...

  3. 【转】centos升级curl版本

    1.安装repo rpm -Uvh http://www.city-fan.org/ftp/contrib/yum-repo/rhel6/x86_64/city-fan.org-release-2-1 ...

  4. 详解python的数字类型变量与其方法

    以下内容引自:https://www.jb51.net/article/97752.htm python数据类型是不允许改变的,这就意味着如果改变 Number 数据类型的值,将重新分配内存空间.下面 ...

  5. Maven笔记一

    简介:什么是maven Maven是一个跨平台的项目管理工具,主要用于基于java平台的项目构建,依赖管理. 如图为项目构建的过程. 解决的项目的问题: 1.  如果有好几个项目,这好几个项目中,需要 ...

  6. JS原型链的理解和使用(二)

    根据在创建对象的时候,创建出来的对象的__proto__指向创建这个对象的函数的prototype属性. 由于在调用对象的属性或者方法的时候会首先在对象的作用域中查找指定的属性或者方法,如果未找到则会 ...

  7. 06. Z字型变换

    题目: 提交01: class Solution { public String convert(String s, int numRows) { int length = 2*numRows-2; ...

  8. spring demo

    参考: https://www.tutorialspoint.com/spring/spring_applicationcontext_container.htm

  9. Java基础 -4

    IF分支结构 在程序开发的过程中 一共会存在有三组程序逻辑:顺序结构.分支结构.循环结构 if分支结构主要是针对关系表达式进行判断处理的分支操作. if判断 public static void ma ...

  10. Python数据类型-4 列表

    列表 列表是Python中最基本也是最常用的数据结构之一.列表中的每个元素都被分配一个数字作为索引,用来表示该元素在列表内所排在的位置.第一个元素的索引是0,第二个索引是1,依此类推. Python的 ...