4 Values whose Sum is 0

Time Limit: 15000MS Memory Limit: 228000K

Total Submissions: 26974 Accepted: 8133

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


解题心得:

  1. 首先要明确跑四重循环不现实,所以正确的方法就是将每一组数分为两半,然后跑双向搜索。可以先得到前两个所有的总和,然后排序,在得到另外两个数的总和,得到的总和就可以在已经排好序的答案中去二分查找这样复杂度就变成了O(n^2logn)。

#include <algorithm>
#include <stdio.h>
#include <cstring>
using namespace std;
const int maxn = 4010;
const int maxm = 2e7+100; int a1[maxn],a2[maxn],a3[maxn],a4[maxn],n;
int sum[maxm]; void init() {
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d%d%d%d",&a1[i],&a2[i],&a3[i],&a4[i]);
} int get_sum() {
int T = 0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
sum[T++] = a1[i] + a2[j];
}
}
sort(sum,sum+T);
return T;
} long long solve(int len) {
int ans = 0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
int k = a3[i] + a4[j];
k = -k;
ans += upper_bound(sum,sum+len,k) - lower_bound(sum,sum+len,k);
}
}
return ans;
} int main() {
init();
int len = get_sum();
long long ans = solve(len);
printf("%lld\n",ans);
return 0;
}

POJ:2785-4 Values whose Sum is 0(双向搜索)的更多相关文章

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

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

  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: 25615   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 ...

随机推荐

  1. rem与em的区别

    这两个单位都是相对元素 rem相对根元素 em相对于父级元素

  2. three.js一个简单demo学些和讲解

    叉口裁剪球体案例 二话不说先上效果图: 全部代码带注释 <!DOCTYPE html> <html lang="en"> <head> < ...

  3. Github站点搭建 gh-pages

    首先:把完整代码放在 gh-pages 分支上,设置 gh-pages 为默认分支(习惯性设置,也可以不设置). 网址: http://你的github域名.github.io/项目入口文件夹/ 本宝 ...

  4. iOS - 通过view查找所在(viewController)

    - (UIViewController *)findViewController:(UIView *)sourceView { id target=sourceView; while (target) ...

  5. Ienumerable和Ienumerator的使用

    using UnityEngine; using System.Collections; public class TestCoroutine : MonoBehaviour { void Start ...

  6. 【起航计划 017】2015 起航计划 Android APIDemo的魔鬼步伐 16 App->Alarm->Alarm Controller Alarm事件 PendingIntent Schedule AlarmManager

    Alarm Controller演示如何在Android应用中使用Alarm事件,其功能和java.util.Timer ,TimerTask类似.但Alarm可以即使当前应用退出后也可以做到Sche ...

  7. Struts1.x 基本原理及注册模块的实现

    1.编写JavaBean:User,必须继承于ActionForm类 package myuser; import org.apache.struts.action.ActionForm; publi ...

  8. C#面向对象几组关键字的详解(this,base,dynamic,var,const,readonly,is,as)

    × 目录 [1]this和base的区别 [2]var和dynamic的区别 [3]const和readonly的区别 [4]is和as的区别 这几个关键字,在用法上有许多相似之处.这里主要看看细节之 ...

  9. Visual Studio 各个版本汇总

    微软开发人员,对开发工具的熟练程度,在一定程度上说明了开发 版本 名称 内部版本 发布日期 支持 .NET Framework 版本 备注 引入 .NET Framework 前[4]  1 Visu ...

  10. python数组列表、字典、拷贝、字符串

    python中字符串方法 name = "I teased at life as if it were a foolish game" print(name.capitalize( ...