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


题目大意:给4列数,每列有n个数字,从每列中取一个数,求四个数相加为零的情况有多少种。
思路:用两个数组,分别记录左边两列和右边两列的数相加的和,再将其中一个数组从小到大排列,将另一数组遍历一边,用二分的方法判断个数。
#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. Tomcat设置编码问题

    为了解决编码问题,在tomcat的server.xml文件中添加了useBodyEncodingForURI="true"配置,如下 <Connector port=&quo ...

  2. TensorFlow在win10上的安装与使用(三)

    本篇博客介绍最经典的手写数字识别Mnist在tf上的应用. Mnist有两种模型,一种是将其数据集看作是没有关系的像素值点,用softmax回归来做.另一种就是利用卷积神经网络,考虑局部图片像素的相关 ...

  3. python最大最小距离算法贴近度评价法

    1.大最小贴近度评价法 概念: 贴近度表示两个模糊几何之间的彼此接近程度,在模糊模式识别方法中采用贴近度的大小识别待判别模糊子集的模式类别.为衡量待识别子集的类别,需要判别各个阶段与标杆模糊集合之间的 ...

  4. Linux基础-yum软件包管理

    任务目标:自定义yum仓库:createrepo,自定义repo文件,使用yum命令安装httpd软件包,卸载httpd软件包:yum –y remove 软件名 ,使用yum安装组件'KDE 桌面' ...

  5. 零值比较--BOOL,int,float,指针变量与零值比较的if语句

    这是程序员面试的一道常见题,也是个C++基础问题.若只在大学里看过几本基础的编程入门书,看见这道题可能会觉得奇怪,不就是和0比较吗,直接拿出来比就是了,其实非也.下文引自google搜索结果,出处不详 ...

  6. qt 零星笔记

    1.qt中堆栈对象的销毁 名字不对,我不知道该取个什么名字,暂且这样吧 在linux c编程中谈到过进程的内存映像,一个进程在内存中的映像如下

  7. 如何基于Spring Boot搭建一个完整的项目

    前言 使用Spring Boot做后台项目开发也快半年了,由于之前有过基于Spring开发的项目经验,相比之下觉得Spring Boot就是天堂,开箱即用来形容是绝不为过的.在没有接触Spring B ...

  8. 采用dlopen、dlsym、dlclose加载动态链接库【总结】【转】

    转自:https://www.cnblogs.com/Anker/p/3746802.html 1.前言 为了使程序方便扩展,具备通用性,可以采用插件形式.采用异步事件驱动模型,保证主程序逻辑不变,将 ...

  9. re-sign重签名

    准备: ① re-sign.jar重签名工具:(下载地址为:http://troido.de/downloads/category/1): ② 从D:\Android\sdk\build-tools\ ...

  10. ASP防XSS代码

    原作是在GitHub上,基于Node.js所写.但是..ASP的JS引擎跟V8又有些不同..于是,嗯.. <% Function AntiXSS_VbsTrim(s) AntiXSS_VbsTr ...