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

Source

[Submit]   [Go Back]   [Status]  
[Discuss]

有时候问题的规模比较大,无法枚举所有元素的组合,但能够枚举一般元素的组合。此时,将问题拆成两半后分别枚举,再合并他们的结果这一方法往往非常有效。

//折半枚举(双向搜索)poj2785
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=5005;
int n;
ll a[maxn],b[maxn],c[maxn],d[maxn];
ll cd[maxn*maxn]; void solve()
{
//枚举cd的组合
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cd[i*n+j]=c[i]+d[j];
}
}
sort(cd,cd+n*n);
ll res=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
ll CD=-(a[i]+b[j]);
//二分搜索取出cd中和为CD的部分
res+=upper_bound(cd,cd+n*n,CD)-lower_bound(cd,cd+n*n,CD);
}
}
printf("%lld\n",res);
} int main()
{
cin>>n;
for(int j=0;j<n;j++)
{
cin>>a[j]>>b[j]>>c[j]>>d[j];
}
solve();
return 0;
}

折半枚举(双向搜索)poj27854 Values whose Sum is 0的更多相关文章

  1. POJ2785-4 Values whose Sum is 0

    传送门:http://poj.org/problem?id=2785 Description The SUM problem can be formulated as follows: given f ...

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

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

  3. UVA 1152 4 Values whose Sum is 0 (枚举+中途相遇法)(+Java版)(Java手撕快排+二分)

    4 Values whose Sum is 0 题目链接:https://cn.vjudge.net/problem/UVA-1152 ——每天在线,欢迎留言谈论. 题目大意: 给定4个n(1< ...

  4. POJ:2785-4 Values whose Sum is 0(双向搜索)

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

  5. [poj2785]4 Values whose Sum is 0(hash或二分)

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

  6. UVA1152-4 Values whose Sum is 0(分块)

    Problem UVA1152-4 Values whose Sum is 0 Accept: 794  Submit: 10087Time Limit: 9000 mSec Problem Desc ...

  7. 4 Values whose Sum is 0(二分)

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

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

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

  9. POJ 2785 4 Values whose Sum is 0

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

随机推荐

  1. Codeforces 631C Report【其他】

    题意: 给定序列,将前a个数进行逆序或正序排列,多次操作后,求最终得到的序列. 分析: 仔细分析可以想到j<i,且rj小于ri的操作是没有意义的,对于每个i把类似j的操作删去(这里可以用mult ...

  2. [bzoj4131]并行博弈_博弈论

    并行博弈 bzoj-4131 题目大意:题目链接. 注释:略. 想法:我们发现无论如何操作都会使得$(1,1)$发生改变. 所以单个$ACG$的胜利条件就是$(1,1)$是否为黑色. 如果为黑色那么可 ...

  3. CODEFORCES problem 105A.Transmigration

    题目本身上手并不难,字符串处理+简单的排序.要注意的地方是浮点数的处理. 依据计算机中浮点数的表示原理,在实际编程的过程中即使用一个确定的整数(假设是1)给一个浮点变量赋值 在查看变量时会发现实际存储 ...

  4. Spring基础入门(一)

    一.Spring概念 1.什么是Spring   Spring是一个开源框架,它由Rod Johnson创建.它是为了解决企业应用开发的复杂性而创建的.Spring使用基本的JavaBean来完成以前 ...

  5. Telnet登入cisco router 1800

    Login to Router and change to privileged modec:\>telnet 192.168.6.1Trying 192.168.6.1...Connected ...

  6. 使用requireJS的shim參数,完毕jquery插件的载入

    没有requireJS框架之前,假设我们想使用jquery框架,会在HTML页面中通过<script>标签载入.这个时候jquery框架生成全局变量$和jQuery等全局变量.假设项目中引 ...

  7. C++之new和malloc差别

         在C++程序猿面试中.非常easy被问到new 和 malloc的差别.偶尔在quora上逛.看到Robert Love的总结.才发现自己仅仅知道里面的一两项就沾沾自喜,从来没有像这位大牛一 ...

  8. 【Poj3126】【BNUOJ3245】Prime Path

    http://poj.org/problem?id=3126 https://www.bnuoj.com/v3/problem_show.php?pid=3245 题目鬼图 刚开始看到题目的图觉得这题 ...

  9. build_native.py文件分析(2)

    def build(ndk_build_param,android_platform,build_mode): ////获取ndk_root -- NDK_ROOT = os.environ['NDK ...

  10. expandableListview的默认箭头箭头怎样移到右边

    1 . ExpandableListView布局:<ExpandableListView    android:id="@+id/bbs_category_expandable_lis ...