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 ...
随机推荐
- github删除已经push到服务器上的commit的方法
使用两条指令: git reset --hard <commit_id> git push origin HEAD --force 其中commit_id是你想回到的commit的id(即 ...
- iOS Search bar 输入空字符串也可以搜索
Search bar delegate - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { UITextField *sea ...
- Cocos2d-x 学习之引擎介绍
Cocos2d-X是一个开源的移动2D游戏框架,MIT许可证下发布的.这是一个C + +cocos2d-iPhone项目的版本.cocos2d-X发展的重点是围绕cocos2d跨平台.即其实现一次编码 ...
- spring + quartz 定时
springConfig配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...
- debian各种包
1 zlib compression library sudo apt-get install zlib1g-dev 2 c-ares库 libc-ares-dev - asynchronous na ...
- event chrome firefox 获取点击对象的 id 类
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...
- IO流入门-第八章-BufferedWriter
BufferedWriter基本用法和方法示例 import java.io.*; public class BufferedWriterTest01 { public static void mai ...
- 好难忘又伤心一个非常好的学习js群解散了
不知道为什么看到web前端之天天向上这个群解散,好难过.应该可以说,这个群是我见过比较靠谱的群,大家都非常热情帮助.那些管理员管理的非常好,突然解散了.觉得好可惜,也不会因为你是菜鸟不让你加,感觉好可 ...
- Python设计模式-装饰器模式
装饰器模式 装饰器模式,动态地给一个对象添加一些额外的职责,就增加功能来说,装饰器模式比生成子类更为灵活. 代码示例 #coding:utf-8 #装饰器模式 class Beverage(): ...
- git学习------>写给 Git 初学者的7个建议
PS:本文转载于(http://blog.jobbole.com/50603/),本文由 伯乐在线 - 吴鹏煜 翻译. 英文出处:(http://sixrevisions.com/web-develo ...