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. Educational Codeforces Round 82 A. Erasing Zeroes

    You are given a string ss. Each character is either 0 or 1. You want all 1's in the string to form a ...

  2. ECMAScript 6 和数组的新功能

    Array. @@iterator 返回一个包含数组键值对的迭代器对象,可以通过同步调用得到数组元素的键值对 copyWithin 复制数组中一系列元素到同一数组指定的起始位置 entries 返回包 ...

  3. 【原】centos安装django

    一.更新系统软件包yum update -y 二.安装软件管理包和可能使用的依赖 yum -y groupinstall "Development tools" yum insta ...

  4. Java面向对象编程 -1.3

    类与对象的定义与使用 在Java之中类是一个独立的结构体,所以需要使用class来进行定义,而类之中主要由属性和方法所组成,那么属性就是一个个具体的变量,方法就是可以重复执行的代码. 定义一个类 cl ...

  5. I/O-<File区别>

    FileInputStream           FileOutputStream ObjectInputStream      ObjectOutputStream  传的是对象    需要新建一 ...

  6. 洛谷 T2691 桶哥的问题——送桶

    嗯... 题目链接:https://www.luogu.org/problem/T2691 这道题有一点贪心的思想吧...并且思路与题目是倒着来的(貌似这种思路已经很常见的... 先举个栗子: 引出思 ...

  7. Fedora26安装Mysql实记(包含yum换源教程)

    课程实验中有一项是安装Hive,这个过程中需要安装Mysql. 在安装Mysql耗费了好多时间,总是报错数据仓库同步失败什么的. 输入yum install mysql 就提示错误,连接不上数据源.该 ...

  8. Start from here: <<OpenGL的基本程序解析>>

    这是我的第一篇学习OpenGL的笔记,也是博主的第一篇博客,希望能够在这里和大家一起成长. 下面的代码是<OpenGL超级宝典(第五版)>中的示例代码,基本程序如下: #include & ...

  9. Java日期时间API系列9-----Jdk8中java.time包中的新的日期时间API类的Period和Duration的区别

    1.Period final修饰,线程安全,ISO-8601日历系统中基于日期的时间量,例如2年3个月4天. 主要属性:年数,月数,天数. /** * The number of years. */ ...

  10. 计算机基础 - 时间戳(timestamp)位数

    分为10位数字(ten digit)和13位(thirteen digit)数字 1. Unix, Python为10 time +%s import time time.time() 2. Java ...