题目链接:

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. bootstrap2.1相关文档

    本节课我们主要学习一下 Bootstrap表格和按钮功能,通过内置的 CSS定义,显示各种丰富的效果. 一.表格 Bootstrap提供了一些丰富的表格样式供开发者使用. 1.基本格式 //实现基本的 ...

  2. bootstrap table 前端搜索

    1.bootstrap-table对于前端的搜索可以通过官网设置,但发现前端搜索出现bug,网上找到一个bootstrap-table的扩充js  bootstrap-table-mytoolbar. ...

  3. cxgrid动态多表头

    function TForm15.CreateBand(View: TcxGridDBBandedTableView;  BandCaption, ParentBandCaption: String) ...

  4. HTML超级链接详细讲解

    超级链接是网站中使用比较频繁的HTML元素,因为网站的各种页面都是由超级链接串接而成,超级链接完成了页面之间的跳转.超级链接是浏览者和服务器的交互的主要手段,在后面的技术中会逐步深化学习. —  注意 ...

  5. headpq

    从一个集合中获得最大或者最小的N个元素列表 http://python3-cookbook.readthedocs.io/zh_CN/latest/c01/p04_find_largest_or_sm ...

  6. C#生成指定长度随机数

    public static string GetRandomString(int iLength) { ";// 随机字符中也可以为汉字(任何) StringBuilder sb = new ...

  7. 重写TreeView,自定义图标,生成通行的下划线,取消默认获得焦点失去焦点的效果,并支持拖拽节点到外界

    1.运行效果: 2.前端代码 <UserControl x:Class="iPIS.UI.Base.Tree.VideoTreeControl" xmlns="ht ...

  8. PICE(1):Programming In Clustered Environment - 集群环境内编程模式

    首先声明:标题上的所谓编程模式是我个人考虑在集群环境下跨节点(jvm)的流程控制编程模式,纯粹按实际需要构想,没什么理论支持.在5月份的深圳scala meetup上我分享了有关集群环境下的编程模式思 ...

  9. Python系列之——利用Python实现微博监控

    0x00 前言: 前几个星期在写一个微博监控系统 可谓是一波三折啊 获取到微博后因为一些字符编码问题 导致心态爆炸开发中断 但是就在昨天发现了另外一个微博的接口 一个手机微博的接口https://m. ...

  10. new关键字创建对象带不带{}的区别

    gson通过TypeToken实现了对泛型数据的支持,使用方式如下: gson.fromJson([待转化的字符串], new TypeToken<[目标类]<目标类中的泛型>> ...