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 - 二分折半查找的更多相关文章

  1. POJ - 2785 4 Values whose Sum is 0 二分

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25615   Accep ...

  2. POJ 2785 4 Values whose Sum is 0(折半枚举+二分)

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 25675   Accep ...

  3. 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 ...

  4. POJ 2785 4 Values whose Sum is 0(想法题)

    传送门 4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 20334   A ...

  5. POJ 2785 4 Values whose Sum is 0

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 13069   Accep ...

  6. 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 ...

  7. POJ 2785 4 Values whose Sum is 0(哈希表)

    [题目链接] http://poj.org/problem?id=2785 [题目大意] 给出四个数组,从每个数组中选出一个数,使得四个数相加为0,求方案数 [题解] 将a+b存入哈希表,反查-c-d ...

  8. 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 ...

  9. [POJ] 2785 4 Values whose Sum is 0(双向搜索)

    题目地址:http://poj.org/problem?id=2785 #include<cstdio> #include<iostream> #include<stri ...

随机推荐

  1. dubbo 序列化 问题 属性值 丢失 ArrayList 解决

    参考文章:http://blog.csdn.net/wanyanxgf/article/details/6944733 http://tianya23.blog.51cto.com/1081650/5 ...

  2. 《从零开始学Swift》学习笔记(Day60)——Core Foundation框架

    原创文章,欢迎转载.转载请注明:关东升的博客 Core Foundation框架是苹果公司提供一套概念来源于Foundation框架,编程接口面向C语言风格的API.虽然在Swift中调用这种C语言风 ...

  3. POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1423 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  4. for...of 与 for...in 区别

    一.for...of 1.定义 for...of 语句遍历可迭代对象(包括数组.Set 和 Map 结构.arguments 对象.DOM NodeList 对象.字符串等). 2.语法 for (v ...

  5. Java 语言基础之数组常见操作

    对数组操作最基本的动作: 存和取 核心思想: 就是对角标的操作 数组常见操作: 1, 遍历 2, 获取最大值和最小值 3, 排序 4, 查找 5, 折半查找 // 1. 遍历 int[] arr = ...

  6. 如何在 window 上面输入特殊字符?

    打开 字符映射表 程序 选中任意一个字符,它会在下方显示该字符的 16进制 转换16进制至10进制,并在输入法打开的状态下,按住 Alt 键输入 10 进制数值即可.

  7. Submission Details [leetcode] 算法的改进

    最先看到这一题,直觉的解法就是len从1到s1.size()-1,递归调用比較s1和s2长度为len的子串是否相等.以及剩余部分是否相等. 将s1分成s1[len + rest],分别比較s2[len ...

  8. 基于mondrian聚合表的R计算olap开发

    原文出处:http://www.cnblogs.com/qiaoyihang/p/7348328.html 最近在做基于Mondrian的olap开发,总结一下! 一. schema构建 1.思考:我 ...

  9. QT Creator常用快捷键

    1.F10,F11 单步调试 2.Ctrl + / :注释/取消注释选定内容. 3.F4 :在 头文件(.h) 和 实现文件(.cpp) 之间进行切换. 4.Ctrl + i :自动格式化选中代码. ...

  10. jQuery Mobile 手动显示ajax加载器

    在jquery mobile开发中,经常需要调用ajax方法,异步获取数据,如果异步获取数据方法由于网速等等的原因,会有一个反应时间,如果能在点击按钮后数据处理期间,给一个正在加载的提示,客户体验会更 ...