POJ 3368

题意: 给你一组数列, 查询区间内有出现次数最多的数的频数

RMQ ,

对于一个区间, 分为两部分, 从  L 开始连续到  T  , T + 1 到  R

显然 答案为  MAX (T – L + 1 , RMQ ( T+1, R))

对于 T, 可以先预处理出位置 Pos

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 100000 + 131;
int Pos[maxn];
int Num[maxn];
int d[maxn][30];
int Fun[maxn];
int n, m;

void RMQ_init()
{
    for(int i = 0; i < n; ++i) d[i][0] = Fun[i];
    for(int j = 1; (1<<j) <= n; ++j)
        for(int i = 0; i + (1<<j) - 1 < n; ++i)
        d[i][j] = max(d[i][j-1], d[i+(1<<(j-1))][j-1]);
}

int RMQ(int L, int R)
{
    int k = 0;
    while((1<<(k+1)) <= R-L+1) k++;
    return max(d[L][k], d[R-(1<<k)+1][k]);
}

int main()
{
    while(scanf("%d",&n) != EOF)
    {
        if(n == 0) break;
        scanf("%d",&m);
        for(int i = 0; i < n; ++i) scanf("%d",&Num[i]);
        for(int i = 0; i < n; ++i)
        {
            if(i == 0) Fun[i] = 1;
            else
            {
                if(Num[i] == Num[i-1]) Fun[i] = Fun[i-1] + 1;
                else Fun[i] = 1;
            }
        }
        //for(int i = 0; i < n; ++i) cout << Fun[i];
        //cout <<endl;

        ////////////////////////
        int Now = Num[n-1], pos = n-1;
        for(int i = n-1; i >= 0; --i)
        {
            if(Num[i] == Now)
                Pos[i] = pos;
            else
            {
                Now = Num[i];
                Pos[i] = i;
                pos = i;
            }
        }
        /*for(int i = 0; i < n; ++i) cout << Pos[i];
        cout <<endl;*/
        //////////////////////////
        RMQ_init();
        //cout << RMQ(2,5) <<endl;
        int l, r;
        for(int i = 0; i < m; ++i)
        {
            scanf("%d%d",&l,&r);
            l--, r--;
            int t = Pos[l];
            //cout << t << endl;
            if(t >= r)
            {
                printf("%d\n",r-l+1);
                continue;
            }
            else
                printf("%d\n", max(t-l+1,RMQ(t+1,r)));
        }
    }
}
时间: 2024-10-24 10:31:36

POJ 3368的相关文章

poj 3368 Frequent values(线段树解法)

题目链接:http://poj.org/problem?id=3368 题目大意:给你一段不下降的序列,求给定区间里出现次数最多的那个数字的次数. 思路:首先看到这题时,第一感觉线段树,但是仔细一看问题来啦,用线段数我怎么才能计算出某段区间里出现的那个数,因为出现最多的那个数可能不是在他它的左儿子上也不是在它的右儿子上,可能在当他们合并成一个区间时就出现啦,但是这儿我们需要注意的就是,题目给的是一段不下降的序列,那么突破口就出来啦,因为如果出现相同的数字,那么它们一定是连续的.所以我们只需要在普

POJ 3368 Frequent values

Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13051 Accepted: 4792 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisti

poj 3368 Frequent values 解题报告

题目链接:http://poj.org/problem?id=3368 题目意思:给出一段 n 个数的序列你,对于区间 [l, r] 的询问,找出 出现频率最高的数的次数.考虑到序列中的数是非递减的,也就是相同的数会连续不间断地在一起,于是就才有了代码中这个部分来预判了: if (s > t)        printf("%d\n", ans); 这个人写RMQ 写得不错:http://dongxicheng.org/structure/lca-rmq/ 这题就是套模板的,纪念

poj 3368 Frequent values(RMQ)

1 /************************************************************ 2 题目: Frequent values(poj 3368) 3 链接: http://poj.org/problem?id=3368 4 题意: 给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之 5 间连续出现次数最多的次数 6 算法: RMQ 7 思路: 借助数组f[i].表示第i位前面有f[i]个相同的数.对于 8 每个区间(l,r).暴力求前面几个

【POJ 3368】 Frequent values(RMQ)

[POJ 3368] Frequent values(RMQ) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15813   Accepted: 5749 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given seve

poj 3368(RMQ简单应用)

Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13317   Accepted: 4894 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries cons

POJ 3368:Frequent values RMQ

Frequent values 题目链接: http://poj.org/problem?id=3368 题意: 给出一个非递减序列,求区间内最多的数字的数量 题解: 水题,dp[i][j]记录从 i 开始2^j个数中的出现最多的数,合并dp[i][j]和dp[i+(1<<j)][j]得到dp[i][1<<(j+1)]的时候,由于序列是连续的,只要多考虑一个i+(1<<j)-1(即dp[i][j]所能到达的最右端)上得数字在区间内出现的次数就好了. 代码 #includ

[题解]poj 3368 Frequent values

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16537   Accepted: 5981 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indice

POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)

题目链接:http://poj.org/problem?id=3368 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, deter

POJ 3368 Frequent values(线段树区间合并)

[题目链接] http://poj.org/problem?id=3368 [题目大意] 有一个有序序列,要求区间查询出现次数最多的数 [题解] 维护每个区间左端点和右端点,以及左右的长度,还有区间的答案 每次线段合并的时候,对区间数据进行合并即可. [代码] #include <cstdio> #include <algorithm> using namespace std; const int N=100010; struct data{int a,b,l,r,val;}T[N