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. Bugku-CTF社工篇之社工进阶

     

  2. POJ 1204 Word Puzzles(AC自动机)

    这题的数据卡在,如下: 5 5 3 ABCDE FGHIJ KLMNO PQRST UVWXY PQR RS RST puzzle中间的行中可以包含要查询的多个单词.这个问题很好解决,SearchDf ...

  3. Codeforces Round #620 (Div. 2) A. Two Rabbits

    Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a p ...

  4. 【PAT甲级】1059 Prime Factors (25 分)

    题意: 输入一个正整数N(范围为long int),输出它等于哪些质数的乘积. trick: 如果N为1,直接输出1即可,数据点3存在这样的数据. 如果N本身是一个质数,直接输出它等于自己即可,数据点 ...

  5. [WC2018]通道(乱搞,迭代)

    [洛谷题面]https://www.luogu.org/problemnew/show/P4221 这个题以及[CTSC2018 暴力写挂]都有类似的乱搞做法能通过考场数据. 具体搞法就是随一个起点, ...

  6. ES Search API

    Search API 搜索请求 SearchRequest用于与搜索文档.聚合.suggestions相关的任何操作,还提供了在结果文档上请求高亮的方法. 在最基本的表单中,我们可以向请求添加查询: ...

  7. 02使用GitHub远程仓库

    一.远程库配置 由于本地的GIT仓库和GitHub仓库之间的传输是通过SSH加密的,所以需要以下配置: 1.创建SSH key 为什么GitHub需要SSHKey:根据key来授权,有哪些key可以往 ...

  8. 八 Spring的IOC的XML和注解的区别及其整合开发

    xml和注解的区别 xml和注解整合开发 注解:取消扫描配置开启注解配置 扫描:<context:component-scan base-package="" />  ...

  9. Reversing-x64Elf-100----攻防世界

    题目来源:攻防世界 环境:win10 软件:pycharm.64位的ida 常规的操作ida打开查看,看到了main函数,

  10. JSTL中获取URL参数

    使用JSTL时,URL会被隐含的对象param包裹起来,使用param.变量名,直接获取值 <body>hello:${param.name}</body> 依据此逻辑,在使用 ...