【问题描述】

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 × B × C × D are such that a + b + c + d = 0. In the following, we assume that all lists have the same size n.

【输入描述】

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs. 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.

【输出描述】

For each test case, your program has to write the number quadruplets whose sum is zero. The outputs of two consecutive cases will be separated by a blank line.

【样例输入】

1

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

【样例输出】

5

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

题目大意就是从四列中分别选出一个数,然后这四个数相加的和为0,暴力的话肯定会超时。

把c[n]和d[n]求和存放到cd[n*n]中并排序,再看 -(a[i]+b[j]) 是否存在于cd[n*n]中。

也可以将a[i]+b[j]放入一个数组ab[n*n]中。

可以使用upper_bound()和lower_bound()函数查找。

参考链接:关于lower_bound( )和upper_bound( )的常见用法

时间:2020ms

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;const int maxn = 4010;
int a[maxn],b[maxn],c[maxn],d[maxn],cd[maxn*maxn],ab[maxn*maxn];int main()
{int  i,j,T,n,cnt,t,k;scanf("%d",&T);while(T--){k=0;scanf("%d",&n);for(i=0;i<n;i++){scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);}for(i=0;i<n;i++){for(j=0;j<n;j++){cd[k]=c[i]+d[j];ab[k++]=a[i]+b[j];}}sort(cd,cd+k);sort(ab,ab+k);cnt=0;for(i=0;i<k;i++){cnt+=(upper_bound(cd,cd+k,-ab[i])-lower_bound(cd,cd+k,-ab[i]));}printf("%d\n",cnt);if(T)printf("\n");}return 0;
}

时间:2530ms

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;const int maxn = 4010;
int a[maxn],b[maxn],c[maxn],d[maxn],cd[maxn*maxn];int main()
{int  i,j,T,n,cnt,t,k;scanf("%d",&T);while(T--){k=0;scanf("%d",&n);for(i=0;i<n;i++){scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);}for(i=0;i<n;i++){for(j=0;j<n;j++){cd[k++]=c[i]+d[j];}}sort(cd,cd+k);cnt=0;for(i=0;i<n;i++){for(j=0;j<n;j++){t=-(a[i]+b[j]);cnt+=(upper_bound(cd,cd+k,t)-lower_bound(cd,cd+k,t));}}printf("%d\n",cnt);if(T)printf("\n");}return 0;
}

【ACM】Uva 1152 (4 Values whose Sum is 0) 二分查找lower_bound() 和upper_bound()的使用相关推荐

  1. uva 1152 ——4 Values whose Sum is 0

    题意:给定4个n元素集合,要求从每个集合中选择一个数,使得A+B+c+d==0,问存在多少种方法. 思路:枚举+hash判断.直接枚举4次方的算法会超,那么只需要枚举ab,然后在c+d的和中查找等于- ...

  2. UVA 1152 4 Values Whose Sum is Zero 和为0的4个值 (中途相遇)

    摘要:中途相遇.对比map,快排+二分查找,Hash效率. n是4000的级别,直接O(n^4)肯定超,所以中途相遇法,O(n^2)的时间枚举其中两个的和,O(n^2)的时间枚举其他两个的和的相反数, ...

  3. poj2785 4 Values whose Sum is 0

    原题链接: poj2785 4 Values whose Sum is 0 题意:给出元素都为n的4个数组A,B,C,D,要从每个数组中各取一个数,求这四个数和为0的次数,一个数列有多个相同数字时,当 ...

  4. uva1152 - 4 Values whose Sum is 0(hash或STL技巧ac)

    题目大意:给定4个n(1 <= n <= 4000)元素集合A, B, C, D,要求分别从中选取一个元素a, b, c, d,使得a+b+c+d = 0,问有多少种选法. method ...

  5. 【POJ - 2785】4 Values whose Sum is 0 (二分,折半枚举)

    题干: The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, com ...

  6. POJ 2785: 4 Values Whose Sum is 0

    链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21897 题意:就是给你4个数组,现在问你有多少种方案,使得在每个数组中 ...

  7. POJ 2785 4 Values whose Sum is 0

    传送门:http://poj.org/problem?id=2785 解题思路: 从这四个数列中选择的话总有n的4次方中情况,所以全部判断一遍不可行.不过将他们对半分成AB和CD再考虑的话就可以解决了 ...

  8. Python Numpy多维数组.sum(axis=0/1/2...) 详解

    Python Numpy多维数组.sum(axis=0/1/2-) 详解 numpy中axis取值的说明 首先对numpy中axis取值进行说明:一维数组时axis=0,二维数组时axis=0,1,维 ...

  9. mysql sum 为 0 的解决方法

    mysql sum 为 0 的解决方法 参考文章: (1)mysql sum 为 0 的解决方法 (2)https://www.cnblogs.com/huanghuanghui/p/9338037. ...

最新文章

  1. 2021-2027年中国氟磷腈橡胶行业发展形势分析及市场前景规划报告
  2. 网易java二面_网易Java开发面试:一面+二面+三面以及 面试经验总结
  3. Gnome Subtitles:字幕编辑器
  4. linux虚拟机如何加网卡,linux虚拟机添加新的网卡
  5. hdu 5167 Fibonacci(预处理)
  6. 图像色调,饱和度,对比度等相关定义
  7. 理解 Delphi 的类(七) - 认识类的多态
  8. 自己动手 CentOS-6.5 安装Oracle11g R2
  9. SQL Server与Excel数据互导
  10. java毕业设计开题报告论文基于JavaWeb项目实现的高校学生在线选课系统
  11. Warez 入门指南
  12. 蚂蚁金服技术90后:从艺术转投数学,还出版首本TensorFlow中文教材
  13. 双线性光强插值(Gouraud明暗处理)学习笔记
  14. H3C S3600 交换机NTP对等体模式的配置
  15. python 截取一段内容_python正则表达式截取一段内容
  16. 全力以赴助梦起航 | 盐城北大青鸟祝各位中考考生万事胜意前程似锦
  17. 什么软件可以代替sc防火墙_蜂蜜和糖稀有什么区别?糖稀可以用蜂蜜代替么?...
  18. int型和char型之间的类型转换
  19. 独家 | 处理非结构化数据的7个实例(附链接)
  20. 运筹学 知识点总结 (七)

热门文章

  1. mysql数据库常见进阶使用
  2. java 简单万年历_JAVA实现的简单万年历代码
  3. PMP®考试是什么机构
  4. 软件测试培训:高薪测试技术要掌握哪些
  5. Struts2+spring+jdbc 以xml配置形式整合
  6. EOS与以太坊有哪些区别? 1
  7. 呼伦湖国家级自然保护区管理局投放草料保野生黄羊过冬
  8. 所有类是object的子类,但是又可以继承一个其他类解析
  9. 【C#小知识】C#中一些易混淆概念总结(七)---------解析抽象类,抽象方法
  10. 围绕云计算 虚拟化技术又呈现新面貌