题目链接:

https://cn.vjudge.net/problem/POJ-2785

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 2 28 ) 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行的4个数,这四列数分别是A,B,C,D的集合,问有多少组ABCD相加和为0
解题思路 刚开始没读懂题就开始写了,没想到题意是另一个意思,还是按练习要求做题吧。
读懂了题,脑子里马上跳出4重循环,又一看n最大为4000,还是放弃吧。
看了一下分析,先将a+b的结果与其出现的次数放在map容器里,再将c+d的结果与其出现的次数放在map容器里,最后查找一下,
如果存在则累计结果。但是超时,原因是常数较大时使用map也可能超时。
随后在网上看到一种更为巧妙的解法,将C和D的所有结果存放在一个一维数组中,再将其排序,遍历A+B的和,累加在这个二维数组
中的个数即可。
*/ /*解法一 超时!!!
#include<cstdio>
#include<iostream>
#include<map>
using namespace std; int main(){
int T,n,cou,i,j,a[4010],b[4010],c[4010],d[4010];
map<int,int> m1,m2; while(scanf("%d",&n) != EOF)
{
j=0;
for(i=1;i<=n;i++){
scanf("%d%d%d%d",&a[j],&b[j],&c[j],&d[j]);
j++;//不能缩放在上面的一句
} for(i=0;i<n;i++){
for(j=0;j<n;j++){
m1[ a[i]+b[j] ]++;
}
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
m2[ -1*(c[i]+d[j] ) ]++;
}
} map<int,int>::iterator it1,it2;
int ans=0;
for(it1=m1.begin(); it1 != m1.end(); it1++){
it2=m2.find(it1->first);
if(it2 != m2.end()){
ans += (it1->second * it2->second);
}
}
printf("%d\n",ans);
}
return 0;
}*/
//解法二
#include<cstdio>
#include<algorithm>
using namespace std; int cd[*];//一维数组当二维数组用 int main(){
int T,n,cou,i,j,a[],b[],c[],d[],sumab;
long long ans;
while(scanf("%d",&n) != EOF)
{
for(i=;i<n;i++)
scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]); for(i=;i<n;i++){
for(j=;j<n;j++){
cd[i*n+j]=c[i]+d[j];
}
} sort(cd,cd+n*n); ans=;
for(i=;i<n;i++){
for(j=;j<n;j++){
sumab=-*(a[i]+b[j]);
ans += upper_bound(cd,cd+n*n,sumab) - lower_bound(cd,cd+n*n,sumab);
//使用参数,起点+终点+目标值
}
} printf("%lld\n",ans);
}
return ;
}

POJ 2785 4 Values whose Sum is 0(暴力枚举的优化策略)的更多相关文章

  1. POJ 2785 4 Values whose Sum is 0(折半枚举)

    给出四个长度为n的数列a,b,c,d,求从这四个数列中每个选取一个元素后的和为0的方法数.n<=4000,abs(val)<=2^28. 考虑直接暴力,复杂度O(n^4).显然超时. # ...

  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(哈希表)

    [题目链接] http://poj.org/problem?id=2785 [题目大意] 给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数 [题解] 将a+b存入哈希表,反查-c-d ...

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

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

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

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

随机推荐

  1. verilog选择数据类型时常犯的错误

    •    信号可以分为端口信号和内部信号.出现在端口列表中的信号是端口信号,其它的信号为内部信号. •    对于端口信号,输入端口只能是net类型.输出端口可以是net类型,也可以是register ...

  2. 纯净得只剩下字的访问IP查询API

    纯净得只剩下字的访问IP查询API 实用资源 / 2018-02-26 / 3 条评论 看到一个好玩的,就随手收藏一下,本API作用:获取用户真实IP,而获取用户IP常见的坑有两个,开发支付的时候也需 ...

  3. Ubuntu下mount命令的好用处

    mount,也就是挂载.如果是让电脑自己挂载Windows的分区,也就是你直接在文件管理器里点击那些Windows的盘符,系统就会帮助你自动挂载,不过其挂载后的名称太长太复杂,不方便终端操作.所以还是 ...

  4. 转(C# 类似右键菜单弹出窗体)

    文章来自 https://www.cnblogs.com/ahdung/p/FloatLayerBase.html 每天进步一点点 新建类  FloatLayerBase 继承Form, 自己有点小改 ...

  5. 3.怎样将ASP.NET MVC应用程序发布到IIS

    这一篇,教大家怎么将ASP.NET MVC应用程序发布到本地或者IIS中.打开上一篇创建的ASP.NET MVC 5.0应用程序.[PS:上一篇--->2.第一个ASP.NET MVC 5.0应 ...

  6. 《AngularJS深度剖析与最佳实践》笔记: 第二章 概念介绍

    第二章 概念介绍 2.1 什么是UI? 用户界面包括内容(静态信息+动态信息), 外观, 交互. 在前端技术栈中分别由HTML, CSS和JS负责. 进一步抽象, 分别对应于MVC三个主要部分: Mo ...

  7. Duolingo 提高用户留存率的6个手段

    翻译 :马玉洁 欢迎访问网易云社区,了解更多网易技术产品运营经验. 如果你用过"Duolingo"(Duolingo)这个语言教育应用程序,你就会知道它就像一款游戏. 这当然不是巧 ...

  8. SpringCloud实现集群和负载均衡

    Spring cloud是一个基于Spring Boot实现的服务治理工具包,在微服务架构中用于管理和协调服务的. 组成部分 spingcloud的五大神兽 服务发现——Netflix Eureka ...

  9. Flask系列09--Flask中WTForms插件,及自定义验证器

    一.概述 django中的forms组件非常的方便,在flask中有WTForms的组件实现的也是类似的功能, 安装这个插件 二.简单使用 文档地址https://wtforms.readthedoc ...

  10. 连接dubbox注册中心的端口默认是20880

    pyg服务工程  都没用到20880 都没用默认,从20881 开始用,第二个服务工程 的则为20882 <dubbo:protocol name="dubbo" port= ...