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. H5不支持的标签

    <acronym> 定义首字母缩写. <applet>  定义applet <basefont> 定义文档中所有文本的默认颜色.大小和字体.请用CSS代替  < ...

  2. Qt 调用本地浏览器打开URL

    点击Qt某些控件,查找本地浏览器打开前端传递的URL. 方法一:直接写死本地浏览器地址 QString programAdress = "C:\Program Files (x86)\Goo ...

  3. mysql path妙用

    CREATE TABLE `lc_c_user_path` ( `id` INT ( ) UNSIGNED NOT NULL AUTO_INCREMENT, `user_id` VARCHAR ( ) ...

  4. CF 1278C Berry Jam 题解

    Forewords 说实话我是被图吸引进来的23333,图画的真的挺好看. 题意 你从一个梯子下到地下室,梯子左右两边各有 \(n\) 瓶果酱排成一排,果酱口味为草莓味或蓝莓味,你每次只能吃你左边或右 ...

  5. 【快学springboot】14.操作redis之list

    前言 之前讲解了springboot(StringRedisTemplate)操作redis的string数据结构,这篇文章将会讲解list数据结构 list数据结构具有的操作 下图列出了redis ...

  6. Jenkins安装 maven插件

    Maven Artifact ChoiceListProvider (Nexus)Maven Metadata Plugin for Jenkins CI serverMaven Release Pl ...

  7. 2_03_MSSQL课程_查询_分组和连接

    “查” 的三种查询语句 where Group by  having  where 对表起作用 (原始硬盘上的表) 单纯的表 having 对结果起作用(筛选) 缓存,不在文件中 select --第 ...

  8. Java容器Stack

    Stack继承关系 Collection 接口 AbstractCollection AbstractList Vector Stack 方法 public E push 元素在栈顶,最后一个元素 p ...

  9. PIL pip error

    结果显示: 提示——Could not find a version that satisfies the requirement PIL (from versions: )No matching d ...

  10. KDE Plasma 5.17 即将发布

    导读 Plasma 5.17上个月达到了beta版本,而下周将发布Plasma 5.17.0版本!KDE桌面的大更新只有几天了.因此,开发人员一直在整理它,同时也集思广益讨论Plasma 5.18应该 ...