POJ 3368: Frequent values

题意:

给出一个单调非减序列,

询问a[i]~a[j]区间内出现最多的元素出现了多少次


题目链接:http://poj.org/problem?id=3368


算法:

RMQ的典型应用,

以前都是用线段树实现的RMQ,

今天特意学习了一下O(nlogn+q)的RMQ算法


代码如下:

//其实我是来存模板的难道我会到处乱说?
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<climits>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;

const int MAXN=110000;
const int MAXM=18;
int rmq[MAXN][MAXM],a[MAXN],pre[MAXN],nxt[MAXN];
vector<int>hash;

int main() {
    int n;
    while(scanf("%d",&n)==1&&n) {
        int q;
        scanf("%d",&q);
        hash.clear();
        memset(rmq,0,sizeof(rmq));
        for(int i=0; i<n; i++) {
            scanf("%d",&a[i]);
            hash.push_back(a[i]);
        }
        hash.erase(unique(hash.begin(),hash.end()),hash.end());
        for(int i=0; i<n; i++) {
            a[i]=lower_bound(hash.begin(),hash.end(),a[i])-hash.begin();
            rmq[a[i]][0]++;
        }
        pre[0]=-1;
        for(int i=0; i<n; i++) {
            if(a[i]==a[i-1]) {
                pre[i]=pre[i-1];
            } else {
                pre[i]=i-1;
            }

        }
        nxt[n-1]=n-1;
        for(int i=n-2; i>=0; i--) {
            if(a[i]==a[i+1]) {
                nxt[i]=nxt[i+1];
            } else {
                nxt[i]=i+1;
            }
        }
        int cot=hash.size();
        for(int j=1; j<MAXM; j++) {
            for(int i=0; (i+(1<<j))<=cot; i++) {
                rmq[i][j]=max(rmq[i][j-1],rmq[i+(1<<(j-1))][j-1]);
            }
        }
        while(q--) {
            int x,y;
            scanf("%d%d",&x,&y);
            x--;
            y--;
            if(a[x]==a[y]) {
                printf("%d\n",y-x+1);
                continue;
            }
            int tmp=nxt[x]-x;
            x=a[x]+1;
            tmp=max(tmp,y-pre[y]);
            y=a[y]-1;
            if(x>y) {
                printf("%d\n",tmp);
                continue;
            }
            int k=(int)log2(y-x+1);
            tmp=max(tmp,max(rmq[x][k],rmq[y-(1<<k)+1][k]));
            printf("%d\n",tmp);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值