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

【题意】给出一个n*4的矩阵,每列上选一个数使得最后加起来为0,问有多少种取法

【思路】先用ab数组存a+b的所有组合,同理,存储cd数组,然后对cd数组进行排序,然后用upper_bound,lower_bound查找是否存在-ab[i],正好两者只差为1,即多了一种组合方式

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int N=+;
int n;
int a[N],b[N],c[N],d[N];
int ab[N*N],cd[N*N];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
}
int k=;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
ab[k]=a[i]+b[j];
cd[k]=c[i]+d[j];
k++;
}
}
sort(cd,cd+k);
long long ans=;
for(int i=;i<k;i++)
{
int tmp=-ab[i];
ans+=(long long )(upper_bound(cd,cd+k,tmp)-lower_bound(cd,cd+k,tmp));
}
printf("%I64d\n",ans);
return ;
}

4 Values whose Sum is 0_upper_bound&&ower_bound的更多相关文章

  1. POJ 2785 4 Values whose Sum is 0(想法题)

    传送门 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 20334   A ...

  2. POJ 2785 4 Values whose Sum is 0

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

  3. 哈希-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: ...

  4. [poj2785]4 Values whose Sum is 0(hash或二分)

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

  5. K - 4 Values whose Sum is 0(中途相遇法)

    K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS     Memory Limi ...

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

  7. UVA1152-4 Values whose Sum is 0(分块)

    Problem UVA1152-4 Values whose Sum is 0 Accept: 794  Submit: 10087Time Limit: 9000 mSec Problem Desc ...

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

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

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

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

随机推荐

  1. 手动实现KVO

    前言 KVO(Key-Value Observing, 键值观察), KVO的实现也依赖于runtime. 当你对一个对象进行观察时, 系统会动态创建一个类继承自原类, 然后重写被观察属性的sette ...

  2. poi 读取 excel(.xlsx) 2007及以上版本

    1.注意的一点是sh.getLastRowNum(),比实际的行数少一行 涉及到的包:

  3. 用ajax向处理页面传送路径问题解决方法

    var pic = document.getElementById("pic");    var ppp = pic.getAttribute("src");/ ...

  4. 转: Jsp9个内置对象详解

    1.request对象 客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求, 然后做出响应.它是HttpServletRequest类的实例. 序号方法说明 objectgetA ...

  5. 告别硬编码-发个获取未导出函数地址的Dll及源码

    还在为找内核未导出函数地址而苦恼嘛? 还在为硬编码通用性差而不爽吗? 还在为暴搜内核老蓝屏而痛苦吗? 请看这里: 最近老要用到内核未导出的函数及一些结构,不想再找特征码了,准备到网上找点符号文件解析的 ...

  6. CodeIgniter 让控制器可以支持多级子目录的 Router 类库

    MY_Router.php 放到 system/application/libraries 目录下,就可以让 CI 的控制器支持多级子目录了.这样,你就可以在 system/application/c ...

  7. DOM 之 SAX操作

    SAX采用部分读取的方式,可以进行大型文件的处理,而且只需要从文件中读取特定的内容,SAX解析可以由用户自己建立对象模型.

  8. DotNetBar v12.2.0.7 Fully Cracked

    PS: 博客园的程序出现问题,导致我的博客不能访问(转到登录页),而我自己由于 Cookies 问题,一直可以访问,所以一直未发现该问题. 感谢冰河之刃告知,thx! 更新信息: http://www ...

  9. C/C++遍历Windows文件夹下的所有文件

    因为文件夹中往往包含文件和文件夹.想要遍历所有的文件,必须遍历文件夹中所有的文件夹.很显然,这个描述满足递归的两个要素:(1)问题的规模在不断的缩小,且新问题的模式与旧问题相同.很显然文件夹中含有子文 ...

  10. Word2013可以写博客

    步骤如下http://www.cnblogs.com/guyichang/p/4629211.html