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. vue组件双向绑定.sync修饰符的一个坑

    我们知道组件是单项的,但是有时候需要双向,这时候我们可以使用.sync修饰符,但今天遇到一个坑,一直不成功,花了半小时试出来的.... 在编程的时候我们很习惯冒号后面跟着空格.而.sync双向绑定需要 ...

  2. [RabbitMQ]Windows环境下rabbitmqclt(Command Line Tools)出现Erlang distribution failed错误的解决方法

    摘要 当使用rabbitmqctl时出现Erlang distribution failed,把%SystemRoot%Windows\System32\config\systemprofile下的. ...

  3. 【起航计划 020】2015 起航计划 Android APIDemo的魔鬼步伐 19 App->Dialog Dialog样式

    这个例子的主Activity定义在AlertDialogSamples.java 主要用来介绍类AlertDialog的用法,AlertDialog提供的功能是多样的: 显示消息给用户,并可提供一到三 ...

  4. java环境变量配置备忘录

    用鼠标右击“我的电脑”->属性->高级->环境变量系统变量->新建->变量名:JAVA_HOME 变量值:D:\Program Files\Java\jdk1.6.0_1 ...

  5. centreon-engine 性能调优

    http://documentation.centreon.com/docs/centreon-engine/en/latest/user/configuration/best_practice.ht ...

  6. 必须夸夸Sublime,大文件打开

    今天有个问题的事情日志文件67.8M大文件打开问题开始: 1.vscode必须挨批:直接就给个错误the file cannt be displayed in the editor because i ...

  7. PHP : 封装Mysqli的连接、关闭和增改查(面向过程)

    废话不多说,直接上图和案例:(看行数进行拼接) 注意:连接数据库中,因为用了$CONNECT全局变量,所以我们的连接数据库封装方法必须要执行了才能获取到此全局变量的值 所引入的数据库文件(databa ...

  8. May 04th 2017 Week 18th Thursday

    No matter how far you may fly, never forget where you come from. 无论你能飞多远,都别忘了你来自何方. I never forget w ...

  9. Selenium入门20 等待时间

    自动化过程中有的页面元素加载慢或者需要等待特定条件执行后续步骤,此时需添加等待时间: 1 time.sleep()  固定等待时间,需import time 2 webdriver隐式等待 无需引入包 ...

  10. Xapian简明教程(未完成)

    第一章 简介 1.1 简介 Xapian是一个开源的搜索引擎库,它可以让开发者自定义的开发一些高级的的索引和查找因素应用在他们的应用中. 通过阅读这篇文档,希望可以帮助你创建第一个你的索引数据库和了解 ...