题目链接

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

题意:

给定各有n个整数的四个数列A,B,C,D。要从每个数列中各取出一个数,使四个数的和为0.求出这样的组合的个数,当一个数列中有多个相同的数字时,把它们作为不同的数字看待。

分析:

从四个数列中选择的话总共有n4种情况,所以全部判断一遍不可行。不过将它们对半分成AB和CD再考虑,就可以快速解决了。从两个序列中选择的话只有n2种组合,所以可以进行枚举。先从A、B中选择a,b后,为了使总和为0,则需要从C,D中取出c+d=-a-b。因此先将从A,B中取数字的n^2种方法全部枚举出来,将这些和排好序,这样就可以运用二分搜索了。

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

代码:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,a[4009],b[4009],c[4009],d[4009];
int num[16000000];
int main()
{
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
num[i*n+j]=a[i]+b[j];
sort(num,num+n*n);
long long int ans=0;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
int cd=-(c[i]+d[j]);
ans+=upper_bound(num,num+n*n,cd)-lower_bound(num,num+n*n,cd); }
printf("%lld\n",ans); return 0;
}

4 Values whose Sum is 0 POJ 2785 (折半枚举)的更多相关文章

  1. 4 Values whose Sum is 0 POJ - 2785

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

  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(折半枚举+二分搜索)

    传送门:Problem 2785 题意: 给定 n 行数,每行都有 4 个数A,B,C,D. 要从每列中各抽取出一个数,问使四个数的和为0的所有方案数. 相同数字不同位置当作不同数字对待. 题解: 如 ...

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

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

  6. POJ 2785 4 Values whose Sum is 0

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

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

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

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

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

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

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

随机推荐

  1. caffe2安装教程

    相比于网上的安装教程不如直接看官方安装教程:https://caffe2.ai/docs/getting-started.html?platform=windows&configuration ...

  2. android gradle打包常见问题及解决方案

    背景: 问题: Q1: UNEXPECTED TOP-LEVEL ERROR: java.lang.OutOfMemoryError: Java heap space at com.android.d ...

  3. AppScan工作原理&操作教程

    一.AppScan的工作原理 对一个综合性的大型网站来说,可能存在成千上万的页面.以登录界面为例,至少要输入用户名和密码,即该页面存在两个字段,当提交了用户名和密码等登录信息,网站需要检查是否正确,这 ...

  4. 【题解】51nod1327 棋盘游戏

    那天和机房的同学们一起想了很久,然而并没有做出来……今天看了题解,的确比较巧妙,不过细细想来其实规律还是比较明显,在这里记录一下~ 当天自己做的时候,主要想到的是两点 : 1.按列dp 2.对行进行排 ...

  5. openjudge666:放苹果—题解

    (测试这里的markdown,同时也有纪念意义吧--第一次写的题解) 当时刚学递推的时候做的一道题 oj上的666题 666:放苹果 总时间限制: 1000ms 内存限制: 65536kB 描述 把M ...

  6. fhq_treap 学习笔记

    前言:昨天写NOIp2017队列,写+调辗转了3h+,不知道怎么的,就点进了一个神仙的链接,便在今日学习了神仙的fhq_treap. 简介:fhq_treap功能强大,支持splay支持的所有操作,代 ...

  7. Promise用法总结

    1. Promise的状态   Promise对象有三个状态: 1. 进行中(pending) 2. 成功(resolved) 3. 失败(rejected)   2. 生成一个Promise对象   ...

  8. 51nod 1962 区间计数(单调栈+二分)

    维护两个单调递减的栈,当i加进栈,位置x的数弹出的时候,在另一个栈中找到和这个数一样大的数,计算贡献(x-靠右左端点)*(i-x). #include<iostream> #include ...

  9. [LOJ 6000]搭配飞行员

    link 其实就是一道二分图匹配板子,我们建立$S$,$T$为源点与汇点,然后分别将$S$连向所有正驾驶员,边权为$1$,然后将副驾驶员与$T$相连,边权为$1$,将数据中给出的$(a,b)$,将$a ...

  10. [转载][mysql]mysql字符集干货

    源地址:http://www.blogjava.net/zyskm/archive/2013/04/09/361888.html 字符集的概念大家都清楚,校对规则很多人不了解,一般数据库开发中也用不到 ...