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

题意是给出4列数,从每一列中挑选一个数字,问有多少种方法使这四个数的和为0。

求出每两列的数的和,再二分查找。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#pragma warning(disable:4996)
using namespace std; int a[4005],b[4005],c[4005],d[4005];
int sum1[4005*4005],sum2[4005*4005];
int n; int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int i,j; scanf("%d",&n); for(i=1;i<=n;i++)
{
scanf("%d%d%d%d",a+i,b+i,c+i,d+i);
} int num1=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
sum1[++num1] = -(a[i]+b[j]);
sum2[num1] = (c[i]+d[j]);
}
}
sort(sum1+1,sum1+1+num1);
sort(sum2+1,sum2+1+num1); int ans=0;
sum1[0]= -268435456*2 - 1;
sum1[num1+1] = 268435456*2 + 1;
for(i=1;i<=num1;i++)
{
int left = 0;
int right = num1+1;
int mid;
while(left<right)
{
mid=(left+right)/2;
if(sum2[i]<=sum1[mid])
{
right=mid;
}
else
{
left= mid+1;
}
}
while(sum2[i]==sum1[right]&&right<=num1)
{
ans++;
right++;
}
} printf("%d\n",ans);
//system("pause");
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: 25615   Accep ...

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

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

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

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

  4. POJ 2785 4 Values whose Sum is 0

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 13069   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(暴力枚举的优化策略)

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

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

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

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

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

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

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

随机推荐

  1. 基于Qt 5.9.8,编译Qtxlsx

    1.源码下载地址:https://github.com/dbzhang800/QtXlsxWriter 2.下载并安装编译时需要的工具 Perl:https://www.perl.org/get.ht ...

  2. 使用eclipse部署springcloud config从GitHub上获取配置内容出现错误:Auth fail

    Eclipse点击Window > Preferences > General > Network Connections > SSH2 点击"Key Managem ...

  3. Git远程分支代码强制回退&Tag添加

    Git指令大全:https://www.alexkras.com/getting-started-with-git/ Git提交错了,还是Master分支,哎呦喂咋整?请见下文.   [场景描述] 项 ...

  4. Session 'app': Error Installing APKs app 在手机或虚拟机上调试报错

    解决方案: build --clean project

  5. Hadoop大实验——MapReduce的操作

    日期:2019.10.30 博客期:114 星期三 实验6:Mapreduce实例——WordCount   实验说明: 1.          本次实验是第六次上机,属于验证性实验.实验报告上交截止 ...

  6. Python 中的经典类新式类

    Python 中的经典类新式类 要知道经典类和新式类的区别,首先要掌握类的继承 类的继承的一个优点就是减少代码冗余 广度优先和深度优先,这主要是在多类继承的时候会使用到 经典类和新式类的主要区别就是类 ...

  7. PaperReading20200223

    CanChen ggchen@mail.ustc.edu.cn   AdaBatch Motivation: Current stochastic gradient descend methods u ...

  8. Dockfile的详解

    Dockerfile是构建镜像的指令文件,按照dockerfile的规范分为如下几部分: FROM 基础镜像,FROM alpine:3.8(任何的一个镜像都可以作为基础镜像,主要看我们做的镜像是干嘛 ...

  9. java并发:初探消费者和生产者模式

    消费者和生产者模式 用继承Thread方式,用wait和notifyAll方法实现. 消费者和生产者模式的特点 1. 什么时候生产:仓库没有满的时候,生产者这可以生产,消费者也可以消费,仓库满的时候停 ...

  10. POJ 1487:Single-Player Games 浮点数高斯消元

    Single-Player Games Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1287   Accepted: 36 ...