4 Values whose Sum is 0
Time Limit: 15000MS   Memory Limit: 228000K
Total Submissions: 29243   Accepted: 8887
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). 
 
题意:ABCD四个数列,每个数列抽一个数字,使得和为0,问一共有几组。当一个数列中有多个相同的数字时,把他们作为不同的数字看待。
思路:直接爆搜肯定会超时,将它们对半分成AB和CD再考虑就可以解决。比如在AB中取出a,b后,CD中要取出-a-b,因此先把CD中所有取数字的情况n*n种给纪录下来,然后排序,用二分查找次数。
 
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
typedef long long ll;
const int maxn = ;
const int mod = 1e9 + ;
int gcd(int a, int b) {
if (b == ) return a; return gcd(b, a % b);
} int n;
int a[maxn],b[maxn],c[maxn],d[maxn];
int cd[maxn*maxn]; 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 res=;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
int ab=-(a[i]+b[j]);
res+=upper_bound(cd,cd+n*n,ab)-lower_bound(cd,cd+n*n,ab);
}
cout<<res<<endl;
}
int main()
{
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d %d %d %d",&a[i],&b[i],&c[i],&d[i]); solve();
}

4 Values whose Sum is 0 POJ - 2785的更多相关文章

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

    题目链接 Description The SUM problem can be formulated as follows: given four lists A, B, C, D of intege ...

  2. Divide and conquer:4 Values whose Sum is 0(POJ 2785)

    找四个数的和为0 题目大意:给定四个集合,要你每个集合选4个数字,组成和为0 这题是3977的简单版,只要和是0就可以了 #include <iostream> #include < ...

  3. 4 Values whose Sum is 0 POJ - 2785(二分应用)

    题意:输入一个数字n,代表有n行a,b,c,d,求a+b+c+d=0有多少组情况. 思路:先求出前两个数字的所有情况,装在一个数组里面,再去求后两个数字的时候二分查找第一个大于等于这个数的位置和第一个 ...

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

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

  5. POJ 2785 4 Values whose Sum is 0

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

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

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

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

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

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

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

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

随机推荐

  1. CentOS安装MySQL,(常规安装方式MySQL无法远程连接)

    转载自:https://blog.csdn.net/z13615480737/article/details/78906598 CentOS7默认数据库是mariadb, 但是 好多用的都是mysql ...

  2. springcloud中servcie层调用fegin异常以及异步方法的实现

    近日在做业务上的短信推送和APP消息推送,通过调用别的模块的接口来实现,在springcloud中通过fegin进行调用.这里要说明的事情并不是如何开发推送功能,而是在调试过程中碰到的一些小问题.我把 ...

  3. VMware ESXi5忘记登录密码解决办法

    很久没有登录ESXi5了,今天登录发现忘记密码了: 网上搜索到的方法都是使用linux其他版本的镜像的恢复模式来重置密码(尝试过了,ESXI自己的镜像没有恢复模式).于是使用现有的Ubuntu镜像来操 ...

  4. MySQL学习系列2--MySQL执行计划分析EXPLAIN [原创]

    1.Explain语法 EXPLAIN SELECT …… 变体:   EXPLAIN EXTENDED SELECT …… 将执行计划“反编译”成SELECT语句,运行SHOW WARNINGS 可 ...

  5. 【Shell脚本学习24】Shell输入输出重定向:Shell Here Document,/dev/null文件

    Unix 命令默认从标准输入设备(stdin)获取输入,将结果输出到标准输出设备(stdout)显示.一般情况下,标准输入设备就是键盘,标准输出设备就是终端,即显示器. 输出重定向 命令的输出不仅可以 ...

  6. 编译出freeswitch的java调用的 jar和so

    假设freeswitch 源码路径为 /usr/local/src/freeswitch 1. cd /usr/local/src/freeswitch(源代码的根目录) 执行./configure, ...

  7. isset或array_key_exists,检查数组键是否存在

    今天在导出报表的时候遇到了一个问题,undefined index:pid,然后就纳闷了,我的数组里面根本就没有pid,为什么会出现这个错误呢,我遍历了一下数组,发现果然有pid这个键,奇怪呀,我有做 ...

  8. sql队伍的胜负情况

    1.数据显示情况 2.sql语句执行情况 USE [数据库名] GO /****** Object: Table [dbo].[测试] Script Date: 08/03/2017 10:58:02 ...

  9. jQuery_2_常规选择器-进阶选择器

    进阶选择器: 1. 群组选择器     $("span,em,#box")   获取多个选择器的DOM对象 <div id="d1">div< ...

  10. ARM实验1 —— 流水灯实验

    实验内容: 编写GPIO模块程序,实现对FS_4412平台的上的led2,led3,led4 ,led5,的流水灯实现. 实验目的: 熟悉开发环境的使用. 掌握Exynos 4412处理器GPIO功能 ...