题目链接:

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

    链表是线性表的一种.线性表是最基本,最简单也是最常见的一种数据结构.线性表中数据元素之间的关系是一对一的关系,除了第一个和最后一个数据元素外,其他数据元素都是首尾相接的. 线性表有两种存储方式,一种是 ...

  2. CxGrid筛选自动添加百分号和默认旧的滚动条样式

    CxGrid筛选自动添加百分号和默认旧的滚动条样式 2018-10-29 Delphi 约 693 字  预计阅读 2 分钟 文章目录 cxGrid支持使用like过滤时自动添加百分号 DevExpr ...

  3. Delphi-idHttp-Post JSON用法 good

    从国外网站抄来的代码 Delphi source: http := TIdHttp.Create(nil);http.HandleRedirects := True;//允许头转向http.ReadT ...

  4. 在Asp.Net MVC中利用快递100接口实现订阅物流轨迹功能

    前言 分享一篇关于在电商系统中同步物流轨迹到本地服务器的文章,当前方案使用了快递100做为数据来源接口,这个接口是收费的,不过提供的功能还是非常强大的,有专门的售后维护团队.也有免费的方案,类似于快递 ...

  5. c# WebApi创建及客户端调用

    前段时间学习WebApi的创建与调用,网上的信息千奇百怪(知识有限,看不懂啊),通过查阅资料及借鉴博友实例分析后总结一下,总结一套简单完整的WebApi创建及实例 首先创建一个WebApi服务(流程就 ...

  6. Python做web开发,推荐几个能立马上手的小项目

    Python这门优美的语言是非常适合web开发的,基于Python的Django框架简单便捷且很强大. 那么作为新手该如何上手这门语言?一切不敲代码的学编程手段都是扯淡,今天就推荐一些适合新手练手的P ...

  7. Spring 全局异常处理

    [参考文章]:Spring全局异常处理的三种方式 [参考文章]:Spring Boot 系列(八)@ControllerAdvice 拦截异常并统一处理 [参考文章]:@ControllerAdvic ...

  8. yarn 学习 小记

    官网:https://yarnpkg.com/zh-Hans/docs/installing-dependencies 简介:包管理工具,和npm类似主要特点:快速.安全.可靠 快速:本地安装包后,会 ...

  9. postgresql-日志表

    pg_log,数据库日志表postgresqllog CREATE TABLE postgres_log ( log_time timestamp(3) with time zone, 日志生成时间 ...

  10. requests请求例子

    实例一: class GetSalerInfo(View): def post(self, request): userid = request.POST/GET.get('userid',None) ...