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. iOS中的armv6、armv7、armv7s

    armv6.armv7.armv7s是arm CPU的指令集,原则上是向下兼容的,如:iPhone4sCPU支持armv7,但它会兼容armv6,只是使用armv6指令可能无法充分发挥它的特性.iph ...

  2. jquery的jsonp相关

    <!DOCTYPE html><html><head ><meta charset="utf-8"><script src=& ...

  3. [LintCode] A + B 问题

    Bit-by-Bit summation: class Solution { public: /* * @param a: The first integer * @param b: The seco ...

  4. AsyncTaskLoader设计原理大揭秘

    简介 在Android异步处理之AsyncTaskLoader简单使用中我简单的介绍了一下AsyncTaskLoader的基本用法和使用场景,对AsyncTaskLoader还不是很熟悉的小伙伴可以先 ...

  5. python相关的报错处理

    1.python3.6编译安装完毕后,使用pip3安装virtualenv,提示找不到ssl模块 原因:因为我们少装了openssl-devel依赖包,所以导致编译后的pip3无法找到ssl模块. 解 ...

  6. type属性对jq-post在ie、chrome、ff的兼容

    w http://stackoverflow.com/questions/8834635/post-not-working-in-firefox

  7. sql 锁类型与锁机制

    SQL Server锁类型(SQL)收藏1. HOLDLOCK: 在该表上保持共享锁,直到整个事务结束,而不是在语句执行完立即释放所添加的锁.   2. NOLOCK:不添加共享锁和排它锁,当这个选项 ...

  8. Python的3个方法:静态方法(staticmethod),类方法(classmethod)和实例方法

    Python的方法主要有3个,即静态方法(staticmethod),类方法(classmethod)和实例方法,如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  9. 部署Node.js的应用

    原创:作者 mashihua 最近Node.js很火,让很多的前端看到了可以直接从前端写到后端的希望.但是每次部署一个Node.js的应用却让前端苦恼不已.每次登陆服务器,用自己不熟悉的方式从版本控制 ...

  10. 001-docker概述、架构、window安装、基本测试

    一.概述 Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流 ...