摘要: Treap树 核心是 利用随机数的二叉排序树的各种操作复杂度平均为O(lgn)Treap模板:#include #include #include #include #include #include #include #include #include #include #include #in... 阅读全文
posted @ 2014-09-18 16:59 Estimator 阅读(5925) 评论(1) 推荐(6) 编辑
摘要: 写这篇博客之前,花了许久时间来搞这个SG函数,倒是各路大神的论文看的多,却到底没几个看懂的。还好网上一些大牛博客还是性价比相当高的,多少理解了些,也自己通过做一些题加深了下了解。既然是博弈,经典的NIM游戏不得不提一下,这也是要不断提醒自己别忘了NIM游戏才是SG函数由来的核心关键! 1. 若... 阅读全文
posted @ 2014-09-02 00:49 Estimator 阅读(3329) 评论(3) 推荐(10) 编辑

题意:任意区间求第k大数

思路:

  预处理:利用平方分割(分桶法)把区间切割成B = sqrt(n)大小的一块块,然后每个各自排序。

  二分第k大数x,接着就需要求[l,r]区间中x的排名,与k比较,将两边端点非完整桶的点进行扫描,最多B次,其余每个桶进行二分查找排名,可利用upper_bound(STL)即可快速实现。

评价:

  二分确实坑爹,不过搞了这一题也算对二分查找理解深入了些。

二分正确做法:

对于[0..n-1)有[0..m]满足性质其余不满足, 则应用[l, r)进行二分查找, 最后l一定是正确的。总是保证l不成立,r成立。

l = -1, r = n;
while(l < r-1)
{
    int mid = (l+r)/2;
    if(check(mid))
        l = mid;
    else
        r = mid;
}
cout << l << endl;

而若是[m, n-1]满足性质, 则应用(l, r]进行二分查找, 最后r一定正确,反之即可。

 

保证(l, r]正确性

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <utility>
#include <vector>
#include <queue>
#include <map>
#include <set>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define INF 0x3f3f3f3f
#define MAXN 200005
#define B 1000

using namespace std;

vector<int> bucket[MAXN/B];
int a[MAXN], q[MAXN], n, m, x, y, k;

int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; i ++)
    {
        scanf("%d", &a[i]);
        q[i] = a[i];
        bucket[i/B].push_back(a[i]);
    }
    sort(q, q+n);
    for(int i = 0; i < MAXN/B; i ++)
        sort(bucket[i].begin(), bucket[i].end());
    while(m --)
    {
        scanf("%d%d%d", &x, &y, &k);
        x--, y;
        int l = -1, r = n-1; //(l, r]
        while(l < r-1)
        {
            int mid = (l+r)/2;
            int tx = x, ty = y;
            int cnt = 0;
            for( ; tx < ty && tx%B != 0; tx ++)
                if(a[tx] <= q[mid]) cnt ++;
            for( ; tx < ty && ty%B != 0; ty --)
                if(a[ty-1] <= q[mid]) cnt ++;
            for(int i = tx/B; i < ty/B; i ++)
                cnt += upper_bound(bucket[i].begin(), bucket[i].end(), q[mid])-bucket[i].begin();
            if(k <= cnt)
                r = mid;
            else
                l = mid;
        }
        if(r < 0)
            printf("-1");
        else
            printf("%d\n", q[r]);
    }
    return 0;
}
View Code

 

例如本题若换成[l, r)正确性,稍加改动即可。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <utility>
#include <vector>
#include <queue>
#include <map>
#include <set>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define INF 0x3f3f3f3f
#define MAXN 200005
#define B 1000

using namespace std;

vector<int> bucket[MAXN/B];
int a[MAXN], q[MAXN], n, m, x, y, k;

int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; i ++)
    {
        scanf("%d", &a[i]);
        q[i] = a[i];
        bucket[i/B].push_back(a[i]);
    }
    sort(q, q+n);
    for(int i = 0; i < MAXN/B; i ++)
        sort(bucket[i].begin(), bucket[i].end());
    while(m --)
    {
        scanf("%d%d%d", &x, &y, &k);
        x--, y;
        int l = 0, r = n; //[l, r)
        while(l < r-1)
        {
            int mid = (l+r)/2;
            int tx = x, ty = y;
            int cnt = 0;
            for( ; tx < ty && tx%B != 0; tx ++)
                if(a[tx] < q[mid]) cnt ++;
            for( ; tx < ty && ty%B != 0; ty --)
                if(a[ty-1] < q[mid]) cnt ++;
            for(int i = tx/B; i < ty/B; i ++)
                cnt += lower_bound(bucket[i].begin(), bucket[i].end(), q[mid])-bucket[i].begin();
            if(cnt < k)
                l = mid;
            else
                r = mid;
        }
        printf("%d\n", q[l]);
    }
    return 0;
}
View Code

 

posted @ 2014-12-10 15:52 Estimator 阅读(460) 评论(0) 推荐(0) 编辑
摘要: 极小极大搜索 的个人理解(alpha-beta剪枝)主要算法依据就是根据极大极小搜索实现的。苦逼的是,查了两个晚上的错,原来最终是判断函数写错了。。瞬间吐血!ps. 据说加一句 if sum #include #include #include #include #include #include ... 阅读全文
posted @ 2014-11-16 23:20 Estimator 阅读(622) 评论(0) 推荐(0) 编辑
摘要: /* Author: wsnpyo Update Date: 2014-11-16 Algorithm: 快速幂/Fermat, Solovay_Stassen, Miller-Rabin素性检验/Exgcd非递归版/中国剩余定理*/import randomdef QuickPo... 阅读全文
posted @ 2014-11-16 15:22 Estimator 阅读(1853) 评论(0) 推荐(0) 编辑
摘要: 极小极大搜索的算法过程:参考文档:http://www.xqbase.com/computer/search_minimax.htm(经典) 主要思想比较简单,但说清楚也不大容易。其核心思想是通过对于以后的状态进行预见式的暴搜,对可能的状态进行分析。理论上,如果能够搜索到最终状态,那么之后的走法都... 阅读全文
posted @ 2014-11-16 01:12 Estimator 阅读(6444) 评论(0) 推荐(0) 编辑
摘要: 中国剩余定理的非互质形式任意n个表达式一对对处理,故只需处理两个表达式。x = a(mod m)x = b(mod n)km+a = b (mod n)km = (a-b)(mod n)利用扩展欧几里得算法求出kk = k0(mod n/(n,m)) = k0 + h*n/(n,m)x = km+a... 阅读全文
posted @ 2014-11-14 01:22 Estimator 阅读(641) 评论(0) 推荐(0) 编辑
摘要: POJ 1284求原根个数: 即求 euler(euler(p)) = euler(p-1) 其中p为奇素数 又有 euler(x) = x*(1-1/p1)*...*(1-1/pk) 其中pk为x的质因数#include #include int all, p, ans, num[100000... 阅读全文
posted @ 2014-11-11 17:37 Estimator 阅读(741) 评论(0) 推荐(0) 编辑
摘要: 经过长时间的试验,发现果然学编程还是要学好数学先,数学引发的更加有质的变化,而盲目学各种编程语言也不能获得一种不一样的体验,或者我没掌握到诀窍。另外打算从这学期学的 信安数学基础 学到的庞大的数论体系开刀,上了这课明显体会到彻底自学就是扯蛋。网上找到这份题目,还不错的赶脚,刷起!2014-11-11... 阅读全文
posted @ 2014-11-11 15:36 Estimator 阅读(316) 评论(0) 推荐(0) 编辑
摘要: 编码问题总是梦魂萦绕地折磨着我。 即使每次都能找到一个似乎合理的解释来解释这个编码问题,但是实际上自己明 知还是对于整个计算机体系编码问题不能有一个整体的概念,二进制编码/ASCII编码/Unicode/ANSI编码,还有字符串问题等等。 我们都知道计算机在物理上储存的一切信息,无论是程... 阅读全文
posted @ 2014-10-23 18:16 Estimator 阅读(352) 评论(0) 推荐(0) 编辑
摘要: 本文转自 AstralWind 的博客:Python正则表达式指南 特来收藏 1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分。正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十分强大。得益于这一点, 阅读全文
posted @ 2014-10-21 19:28 Estimator 阅读(6642) 评论(0) 推荐(0) 编辑
摘要: 对于python来说,这两个模块是十分实用的两个模块,以一种简单的方法用于储存数据实例。pickle模块 提供用来储存Python各种数据序列化存储 # 原来的cPickle已经在python3中与pickle合并 dumps(obj) 返回对象信息存储成的二进制字符串 lo... 阅读全文
posted @ 2014-10-21 15:21 Estimator 阅读(942) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示