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 ...
随机推荐
- HDU 5894 hannnnah_j’s Biological Test
题目链接:传送门 题目大意:有n张板凳围成一圈,有m个人,要让m个人都坐到凳子上且任意两人之间相隔>=k 个凳子,问有多少种方法%(1e9+7) 题目思路:组合数学 我们这样考虑,既然每个人相距 ...
- iOS 状态栏更改为白色
如果觉得在iOS 7启动期间状态栏黑色不合你意,以下方法可改变Status bar style成白色 在工程的plist添加 Status bar style,改变style值 默认是Gray sty ...
- java.lang.IllegalArgumentException: Failed to decrypt.
加密失败. 附加信息: org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Co ...
- vue+node+mongoDB 火车票H5(二)---vux和less的配置
vue基本环境配置好之后,就可以开始开发页面了 开发页面之前先了解一下项目结构,原始的目录结构为: config是配置文件,环境配置好了开发阶段可以不再去修改了,node_modules文件夹是打包的 ...
- cocos2d-X学习之主要类介绍:CCDirector
在cocos2d-x里面,游戏的任何时间,只有一个场景对象实例处于运行状态,该对象可以作为当前游戏内容的整体包对象 Cocos2d-x引擎除了提供了CCDirector,还提供了一个CCDisplay ...
- 170208、用Navicat自动备份mysql数据库
数据库备份很重要,很多服务器经常遭到黑客的恶意攻击,造成数据丢失,如果没有及时备份的话,后果不堪设想. 一:备份的目的: 做灾难恢复:对损坏的数据进行恢复和还原 需求改变:因需求改变而需要把数据还原到 ...
- 160801、BlockingQueue处理多线程
前面介绍过spring的taskExecutor,今天介绍一个jdk里处理多线程的方法 一.spring的配置文件(注入bean) <bean id="cmsClickButtonMn ...
- SharePoint服务器端对象模型 之 使用LINQ进行数据访问操作(Part 4)
(六)高效合理的使用LINQ 1.DataContext中的两个属性 为了能够使用DataContext进行数据提交,在DataContext进行数据查询和操作的过程中,内部会进行数据状态的保持和追踪 ...
- 如何设置,使IntelliJ IDEA智能提示忽略大小写
- delphi --批量添加
公共批量添加方法 function BatchSQL(DC : TADOConnection; Qry : TADOQuery; StrSQL : TStrings): Boolean; var i ...