题目链接:

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. RxSwift学习笔记3:生命周期/订阅

    有了 Observable,我们还要使用 subscribe() 方法来订阅它,接收它发出的 Event. let observal = Observable.of("a",&qu ...

  2. Objective-C与Swift混编

    1,创建项目(比如你先选择Objective-C) 2,项目创建成功后接着创建一个swift类  3,Xcode会弹出提示框问你需不需要创建桥接文件(桥接文件的名称默认为:项目名称-Bridging- ...

  3. 12.Scrapy与mongodb交互和设置中间键

    反反爬虫相关机制 Some websites implement certain measures to prevent bots from crawling them, with varying d ...

  4. Delphi程序带参数运行

    程序1 program E1; uses Forms,Dialogs,SysUtils, EndM1 in 'EndM1.pas' {Form2}; {$R *.res} begin Applicat ...

  5. 利用 TFLearn 快速搭建经典深度学习模型

      利用 TFLearn 快速搭建经典深度学习模型 使用 TensorFlow 一个最大的好处是可以用各种运算符(Ops)灵活构建计算图,同时可以支持自定义运算符(见本公众号早期文章<Tenso ...

  6. 合成的默认构造函数定义为delete的一种情况(针对C++11标准)

    1. 默认初始化 如果定义变量时没有指定初值,则变量会被默认初始化,此时变量被赋予了"默认值". 对于类类型的变量来说,初始化都是依靠构造函数来完成的.因此,即使定义某个类的变量( ...

  7. ctags的如何生成tags文件

    tags 在使用vim编程和浏览代码是非常有用.可以用CTRL+]和CTRL+t 来回跳转关键字.先生成自己工作目录的tags.最简单粗暴用法: $cd yourwork $ctags -R * 这样 ...

  8. 在 Ubuntu 16.04上安装 vsFTPd

    在 Ubuntu 16.04上安装 vsFTPd Ubuntu vsFTPd 关于 vsFTPd vsFTPd 代表 Very Secure File Transfer Protocol Daemon ...

  9. Unity 屏幕外死亡的敌人的分数显示在屏幕内

    在敌人死亡后,会出现分数,如果敌人死亡的位置在屏幕内,那么使得获得的分数显示在屏幕内,超出屏幕范围的,显示在屏幕外 当然,这里例子是使得场景中的物体显示在屏幕内,当然也可以使用纯粹的UGUI物体的显示 ...

  10. kali linux 安装sublime text3完全教程

    点击进入官网 下载页面 将鼠标放在64 bit(64位系统)上右击复制链接 打开终端: #wget 路径(粘贴刚复制的) #tar -xvvf 刚刚下载的文件文件名(解压) #mv 解压出来的文件名  ...