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 =  . 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 ). We then have n lines containing four integer values (with absolute value as large as  ) 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

-   -
- -
- -
- - -
- -
- - -

Sample Output


Hint

Sample Explanation: Indeed, the sum of the five following quadruplets is zero: (-, -, , ), (, , -, -), (-, , , -),(-, , -, ), (-, -, , ).

AC代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <set>
using namespace std;
#define ll long long
#define N 4006
int n;
int mp[N][N];
int A[N],B[N],C[N],D[N];
int CD[N*N];
void solve(){
for(int i=;i<n;i++){
for(int j=;j<n;j++){
CD[i*n+j]=C[i]+D[j];
}
}
sort(CD,CD+n*n);
ll ans=;
for(int i=;i<n;i++){
for(int j=;j<n;j++){
int cd = -(A[i]+B[j]);
ans+=upper_bound(CD,CD+n*n,cd)-lower_bound(CD,CD+n*n,cd);
}
}
printf("%I64d\n",ans);
}
int main()
{
while(scanf("%d",&n)==){
for(int i=;i<n;i++){
for(int j=;j<;j++){
scanf("%d",&mp[i][j]);
}
}
for(int i=;i<n;i++){
A[i] = mp[i][];
B[i] = mp[i][];
C[i] = mp[i][];
D[i] = mp[i][];
}
solve();
}
return ;
}

poj 2785 4 Values whose Sum is 0(折半枚举(双向搜索))的更多相关文章

  1. POJ 2785 4 Values whose Sum is 0(折半枚举)

    给出四个长度为n的数列a,b,c,d,求从这四个数列中每个选取一个元素后的和为0的方法数.n<=4000,abs(val)<=2^28. 考虑直接暴力,复杂度O(n^4).显然超时. # ...

  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: 25615   Accep ...

  5. POJ 2785 4 Values whose Sum is 0(折半枚举+二分)

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

  6. POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)

    题目链接:http://poj.org/problem?id=2785 题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数. 其中一个数列有多个相同的数字时 ...

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

  8. POJ 2785 4 Values whose Sum is 0(哈希表)

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

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

随机推荐

  1. qemu-img 快照的一些总结

    qemu-img 快照的一些总结 http://www.openext.org/2014/06/qemu-img-snapshot-re http://blog.csdn.net/muge0913/a ...

  2. xml的加密和解密

    xml加密(XML Encryption)是w3c加密xml的标准.这个加密过程包括加密xml文档的元素及其子元素,通过加密,xml的初始内容将被替换,但其xml格式仍然被完好的保留. 介绍我们有3个 ...

  3. 使用Ksoap2调用Web Service加入SoapHeader

    关于这个问题,如果使用百度都是前篇一律的代码,好不容易上了google才找到完整的方法,这里讲所有的代码都贴出来与大家分享. 首先是.NET写的后台代码 /// <summary> /// ...

  4. NS2仿真:两个移动节点网络连接及协议性能分析

    NS2仿真实验报告2 实验名称:两个移动节点网络连接及协议性能分析 实验日期:2015年3月9日~2015年3月14日 实验报告日期:2015年3月15日 一.实验环境(网络平台,操作系统,网络拓扑图 ...

  5. css实现垂直居中6种方法

    在一次次笔试,一次次的面试中,问到垂直居中的问题太多太多,但是我每一次回答,都好像都不能让面试官太满意,今天特意花点时间,整理一下css垂直居中问题. 1.如果是单行文本.看代码: <!DOCT ...

  6. 关于textbox.attributes["value"]的问题

    在“修改”时,出现这个问题,后台点击修改时,应该是文本框出现一些初始值 BLL.manager bll = new BLL.manager(); Model.manager model = bll.G ...

  7. Swift--基础(二)元组 断言 错误处理

    元组(tuples) 把多个值组合成一个复合值.元组内的值可以是任意类型,并不要求是相同类型 let http404Error = (404, "Not Found") let ( ...

  8. ref和out与SQL中的output

    什么时候会需要使用ref和out 有时,我们会需要获取某个值在方法中的运行状态,根据定义的方法,我们仅仅能够获得一个返回值,但是,有时我们也许想获取多个值,通过返回值就不能返回这样的信息,我们可以通过 ...

  9. 关于javascript中setTimeout()和clearTimeout()的疑惑。

    由于在w3school中学习javascript时,当学到setTimeout()和clearTimeout()方法时.根据它所提供的例子(下面的代码转自w3cschool)—计数程序,发现当你不停的 ...

  10. 你好,C++(20).4.2.2 表达并列条件选择的switch语句:如果……如果……如果……

    4.2.2  表达并列条件选择的switch语句:如果……如果……如果…… 在现实世界中,还有这样一类特殊的条件选择: 如果明天是晴天,我就穿T恤: 如果明天是阴天,我就穿衬衣: 如果明天是雨天,我就 ...