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. python 编程的 Style Guide

    Python 的作者既优雅又高冷又 鬼毛的 再 PEP8 里规定了 Python 程序编写规范.(风格和格式) 一.基本观念 1.可读性之上,代码被读的次数肯定比被写的次数多.因此作者十分重视代码的可 ...

  2. np.multiply

    用法:np.multiply(x1,x2),作用:逐元素相乘,若x1和x2均为标量,则返回标量 x1=np.array([,,]) x2=np.array([,,]) np.multiply(x1,x ...

  3. 增加phpmyadmin导入文件上限

    一.修改php配置 修改php配置文件,php.ini upload_max_filesize = 100M post_max_size = 100M 一般修改这2个就行了,然后重启wampserve ...

  4. java 第三次课后作业

    1.java字段初始化的规律 public class gouzao { public static void main(String[] args) { test te=new test(); Sy ...

  5. python实现队列(queue)

    队列队列是一种先进先出的数据结构,主要操作包括入队,出队.入队的元素加入到对尾,从队头取出出队的元素.这里用列表简单模拟队列,其实现如下: queue()is_empty()size()enqueue ...

  6. Jquery练习1

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. 「JSOI2011」任务调度

    「JSOI2011」任务调度 传送门 一开始还在想写平衡树,看到 \(\text{TRANS}\) 操作后就晓得要用可并堆了. 这题好像就是个可并堆的板子题??? ADD 直接往对应的对里面加元素 D ...

  8. Java Web 前端资源文件的路径问题

    WEB-INF是Java Web应用的安全目录,在部署时用于存放class文件.项目用到的库(jar包).Java Web应用的配置文件web.xml. 浏览器不能访问此目录下的资源,比如在WEB-I ...

  9. Java自学-集合框架 HashMap和Hashtable的区别

    HashMap和Hashtable之间的区别 步骤 1 : HashMap和Hashtable的区别 HashMap和Hashtable都实现了Map接口,都是键值对保存数据的方式 区别1: Hash ...

  10. selenium webdriver 实例化浏览器对象

    public static FirefoxDriver FFSetting() { System.setProperty("webdriver.firefox.bin", &quo ...