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

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

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

## 思路

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 4005;
int cd[maxn*maxn];

int main()
{
int N;
while (~scanf("%d",&N))
{
int a[maxn],b[maxn],c[maxn],d[maxn];
for (int i = 0;i < N;i++)	scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
for (int i = 0;i < N;i++)	for (int j = 0;j < N;j++)	cd[i*N+j] = c[i] + d[j];
sort(cd,cd + N*N);
int res = 0;
for (int i = 0;i < N;i++)
{
for (int j = 0;j < N;j++)
{
int tmp = -a[i] - b[j];
int pos1 = lower_bound(cd,cd + N*N,tmp) - cd;
int pos2 = upper_bound(cd,cd + N*N,tmp) - cd;
res += pos2 - pos1;
}
}
printf("%d\n",res);
}
return 0;
}

## POJ 2785 4 Values whose Sum is 0（想法题）的更多相关文章

1. POJ 2785 4 Values whose Sum is 0

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

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

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

3. POJ 2785 4 Values whose Sum is 0（折半枚举+二分）

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

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

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

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

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

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

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

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

9. POJ 2785 4 Values whose Sum is 0 （二分）题解

思路: 如果用朴素的方法算O(n^4)超时,这里用折半二分.把数组分成两块,分别计算前后两个的和,然后枚举第一个再二分查找第二个中是否有满足和为0的数. 注意和有重复 #include<iost ...

## 随机推荐

1. C++基础——模拟事务 （2）Composite模式

=================================版权声明================================= 版权声明:原创文章 禁止转载  请通过右侧公告中的“联系邮 ...

2. css例子

6.背景图像渐变的制作body{ background:#ccc url(xxx.gif)rpeat-x或y:} 7.给一个区块加上背景#branding{ width:700px: height:2 ...

3. hexo —— 简单、快速、强大的Node.js静态博客框架

hexo是一款基于Node.js的静态博客框架.目前在GitHub上已有1375 star 和 219 fork. 特性 风一般的速度 Hexo基于Node.js,支持多进程,几百篇文章也可以秒生成. ...

4. Javascript手记-基本类型和引用类型

1:ecmascript包含2中不同的数据类型,基本数值类型和引用数值类型.基本数据类型是简单的数据段,引用类型是指那些可能由多个值构成的对象. 1.1:常用的基本类型:Undefined,Null, ...

5. 动态加载资源文件（ResourceDictionary）

原文:动态加载资源文件(ResourceDictionary) 在xaml中控件通过绑定静态资源StaticResource来获取样式Style有多种方式: 1.在项目的启动文件App中<App ...

6. hashMap遍历方式

package Ch17; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java ...

7. Unity UGUI实现鼠标拖动图片

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; ...

8. js及vue监听键盘回车事件

js document.onkeydown = (event) => { var e = event || window.event; if(e && e.keyCode==13 ...

9. C++ 多继承与虚基类

转载来自:CSDN insistGoGo  (http://blog.csdn.net/insistgogo) 多继承的定义:派生类的基类大于一个 语法: class  派生类名:继承方式1 基类名1 ...

10. Spring启动异常: cvc-elt.1: Cannot find the declaration of element &#39;beans&#39;(转)

Spring启动异常: cvc-elt.1: Cannot find the declaration of element 'beans' 2008-09-07 22:41 今天把在线聊天室代码改了下 ...