4 Values whose Sum is 0
 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25615 Accepted: 7697 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

```#include<stdio.h>
#include<algorithm>
using namespace std;
int a[4005][4], sum1[16000001], sum2[16000001];
int main()
{
int n, mid;

while (~scanf("%d", &n))
{
int k = 0;
for (int i = 0; i < n; i++)
{
scanf("%d %d %d %d", &a[i][0], &a[i][1], &a[i][2], &a[i][3]);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
sum1[k] = a[i][0] + a[j][1];
sum2[k++] = a[i][2] + a[j][3];
}
sort(sum1, sum1 + k);

int num = 0;
for (int i = 0; i < k; i++)
{
int left = 0, right = k - 1;
while (left <= right)
{
mid = (left + right) / 2;
if (sum2[i] + sum1[mid] == 0)
{
num++;
for (int j = mid + 1; j < k; j++)
{
if (sum2[i] + sum1[j] == 0)
num++;
else
break;
}
for (int j = mid - 1; j >= 0; j--)
{
if (sum2[i] + sum1[j] == 0)
num++;
else
break;
}
break;
}
if (sum2[i] + sum1[mid] > 0)
right = mid - 1;
else
left = mid + 1;
}
}
printf("%d\n", num);
}
return 0;
}```

## POJ - 2785 4 Values whose Sum is 0 二分的更多相关文章

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

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

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. POJ 2785 4 Values whose Sum is 0（折半枚举+二分）

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

5. POJ 2785 4 Values whose Sum is 0（暴力枚举的优化策略）

题目链接: https://cn.vjudge.net/problem/POJ-2785 The SUM problem can be formulated as follows: given fou ...

6. POJ 2785 4 Values whose Sum is 0（哈希表）

[题目链接] http://poj.org/problem?id=2785 [题目大意] 给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数 [题解] 将a+b存入哈希表,反查-c-d ...

7. POJ 2785 4 Values whose Sum is 0 Hash！

http://poj.org/problem?id=2785 题目大意: 给你四个数组a,b,c,d求满足a+b+c+d=0的个数 其中a,b,c,d可能高达2^28 思路: 嗯,没错,和上次的 HD ...

8. poj 2785 4 Values whose Sum is 0（折半枚举（双向搜索））

Description The SUM problem can be formulated . In the following, we assume that all lists have the ...

9. [POJ] 2785 4 Values whose Sum is 0(双向搜索)

题目地址:http://poj.org/problem?id=2785 #include<cstdio> #include<iostream> #include<stri ...

## 随机推荐

1. a版本冲刺第五天

队名:Aruba   队员: 黄辉昌 李陈辉 林炳锋 鄢继仁 张秀锋 章  鼎 运动会这几天两位同学准备比赛也确实比较忙,两位同学又刚好有事回家去了,由于之前git解决一次冲突后,远程我们还不能很好地 ...

2. Java 异常java.lang.IllegalArgumentException: Illegal group reference

当字符串方法replaceAll()中替换字符含有特殊字符\$如, String test = "<StreamingNo>abc</StreamingNo>" ...

3. ToJson

/// <summary> /// DataTable转成Json /// </summary> /// <param name="jsonName" ...

4. 【leetcode】Remove Duplicates from Sorted List II （middle）

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

5. 【转】【DP_树形DP专辑】【9月9最新更新】【from zeroclock&#39;s blog】

树,一种十分优美的数据结构,因为它本身就具有的递归性,所以它和子树见能相互传递很多信息,还因为它作为被限制的图在上面可进行的操作更多,所以各种用于不同地方的树都出现了,二叉树.三叉树.静态搜索树.AV ...

6. asp.net mvc JQGrid

白色的编辑框看得眼睛不舒服,怎么样更改NotePad++的背景颜色使眼睛更舒服些? 1.设置--语言格式设置 2.设置背景色 “背景色”一栏,选择背景色颜色   “使用全局背景色”一栏要打上√,否则无 ...

8. C++Primer第5版学习笔记（四）

C++Primer第5版学习笔记(四) 第六章的重难点内容         你可以点击这里回顾第四/五章的内容       第六章是和函数有关的知识,函数就是命名了的代码块,可以处理不同的情况,本章内 ...

9. 一步步教你如何源码编译Recovery

*1 准备Ubuntu作为您的操作系统,笔者的版本是12.04_amd64. *2 准备 Android 源码的编译环境,主要是安装一些编译用到的lib库,以及同步源码的一些工具 ,如GIT,CURL ...

10. day3、Linux快捷键及vim命令快捷键

Linux命令行快捷键 快捷键: tab键  自动补全路径 目录  名字,  自动不全命令 快捷键: ctrl +l(小写) 清屏 . ctrl +c 取消当前操作 快捷键: ctrl +d(小写) ...