POJ - 2785 - 4 Values whose Sum is 0 - 二分折半查找
2017-08-01 21:29:14
writer:pprp
参考:http://blog.csdn.net/piaocoder/article/details/45584763
算法分析:直接暴力复杂度过高,所以要用二分的方法,分成两半复杂度就会大大降低;
题目意思:给定4个n(1<=n<=4000)元素的集合 A、B、C、D ,从4个集合中分别选取一个元素a, b,c,d。求满足 a+b+c+d=0的个数
代码如下:
//首先将前两列任意两项相加得到数组x,再将后两列任意两项相加取反得到数组y,
//再将y排序,最后依次将x中的元素在y中进行
//二分查找,看有多少个相等的数加起来即为最终的结果。 #include <iostream>
#include <cstdio>
#include <algorithm> using namespace std; int a[][];
int x[];
int y[];
int ll, ans; //search x[i] from array y[i]
void bit_search(int t)
{
int mid,l = ,r = ll - ;
while(l < r)
{
mid = (l+r) >> ;
if(y[mid] < t)
l = mid + ;
else
r = mid;
}
while(y[l] == t && l < ll) //可能有找到不止一个
{
ans++;
l++;
}
} int main()
{
int n,i,j; while(cin >> n)
{
ans = ,ll = ; for(i = ; i < n; i++) //record the data
cin >> a[i][] >> a[i][]
>> a[i][] >> a[i][]; for(i = ; i < n; i++) //枚举左侧
{
for(j = ; j < n ; j++)
{
x[ll++] = a[i][] + a[j][];
}
} ll = ; for(i = ; i < n ; i++) //枚举右侧
{
for(j = ; j < n ; j++)
{
y[ll++] = -(a[i][] + a[j][]); //这里取反 a + b + c + d = 0等价于a + b = -(c + d);
}
}
sort(y,y+ll); //先排序 for(i = ; i < ll ; i++) //再进行二分查找,如果找到那么ans++
bit_search(x[i]); cout << ans << endl;
}
return ;
}
POJ - 2785 - 4 Values whose Sum is 0 - 二分折半查找的更多相关文章
- POJ - 2785 4 Values whose Sum is 0 二分
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25615 Accep ...
- POJ 2785 4 Values whose Sum is 0(折半枚举+二分)
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 25675 Accep ...
- 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 ...
- POJ 2785 4 Values whose Sum is 0(想法题)
传送门 4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 20334 A ...
- POJ 2785 4 Values whose Sum is 0
4 Values whose Sum is 0 Time Limit: 15000MS Memory Limit: 228000K Total Submissions: 13069 Accep ...
- POJ 2785 4 Values whose Sum is 0(暴力枚举的优化策略)
题目链接: https://cn.vjudge.net/problem/POJ-2785 The SUM problem can be formulated as follows: given fou ...
- POJ 2785 4 Values whose Sum is 0(哈希表)
[题目链接] http://poj.org/problem?id=2785 [题目大意] 给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数 [题解] 将a+b存入哈希表,反查-c-d ...
- 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 ...
- [POJ] 2785 4 Values whose Sum is 0(双向搜索)
题目地址:http://poj.org/problem?id=2785 #include<cstdio> #include<iostream> #include<stri ...
随机推荐
- Asp.Net WebApi核心对象解析
在接着写Asp.Net WebApi核心对象解析(下篇)之前,还是一如既往的扯扯淡,元旦刚过,整个人还是处于晕的状态,一大早就来处理系统BUG,简直是坑爹(好在没让我元旦赶过来该BUG),队友挖的坑, ...
- POJ 3150 Cellular Automaton(矩阵快速幂)
Cellular Automaton Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 3504 Accepted: 1421 C ...
- python之MySQL学习——数据操作
1.增 import pymysql as ps # 打开数据库连接 db = ps.connect(host=', database='test', charset='utf8') # 创建一个游标 ...
- Centos6.5 DNS配置
服务器端:192.168.186.130 1.安装 # yum -y install bind* 2.主要配置文件 [root@localhost named]# vim /etc/named.con ...
- mysqldump迁移说明
使用mysqldump导出数据, 数据包含单行insert,带字段值 #使用mysqldump备份数据到文件, 主要在每个分片的主上面进行备份,确保数据是最新的. mysqldump -h192. - ...
- mysql 中 select中 用case
将 countertype 整数类型转成字符串类型 SELECT counterType, CASE counterType WHEN 1 THEN 'CTP'WHEN 2 THEN 'NULL'WH ...
- Numba makes Python code fast
Numba: A High Performance Python Compiler http://numba.pydata.org/ 一行代码让python的运行速度提高100倍,你信吗?-聚能聊-云 ...
- document.cookie = 'wcookie_date=' + wv + ';max-age=60'
js cookie生命周期
- go项目找不到包问题
最近学习go语言,但是在主函数中引入其他包(自定义包)中方法的时候,编译代码,显示找不到包,如: $GOPATH>go build stacker.gostacker.go:18:2: cann ...
- BFC(Block Formatting Context)基础分析
W3C官方对于BFC的描述只有3小段,强烈建议想理解BFC的朋友先去看看,链接见文末. 常见的文档流分为:定位流.浮动流.普通流3种.BFC是普通流中的一种. 本文提出3个问题并给出使用BFC来解决这 ...