RMQ [HDU 1806] Frequent values

Frequent values

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1146    Accepted Submission(s): 415

Problem 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, determine the most frequent value among the integers ai , ... , aj .

Input

The input consists of several test cases. Each test case starts with a line containing two integers n and q (1 ≤ n, q ≤ 100000). The next line contains n integers a1 , ... , an(-100000 ≤ ai ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces. You can assume that for each i ∈ {1, ..., n-1}: ai ≤ ai+1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the query.

The last test case is followed by a line containing a single 0.

Output

For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.

Sample Input

10 3
-1 -1 1 1 1 1 3 10 10 10
2 3
1 10
5 10
0

Sample Output

1
4
3

Hint

A naive algorithm may not run in time!

Source

HDOJ 2007 Summer Exercise(1)

分成三段、中间RMQ、然后求最大值即可

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define N 100010

int n,m;
int a[N];
int dp[N][20];
int id[N];
int len[N],l[N],r[N];

void init()
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        dp[i][0]=len[i];
    }
    int k=(int)(log((double)n)/log(2.0));
    for(j=1;j<=k;j++)
    {
        for(i=1;i+(1<<j)-1<=n;i++)
        {
            dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
        }
    }
}
int query(int i,int j)
{
    int k=(int)(log((double)(j-i+1))/log(2.0));
    int res=max(dp[i][k],dp[j-(1<<k)+1][k]);
    return res;
}
int main()
{
    int i,pos;
    while(scanf("%d",&n),n)
    {
        pos=0;
        scanf("%d",&m);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            if(a[i]!=a[i-1])
            {
                pos++;
                l[pos]=i;
            }
            id[i]=pos;
            r[pos]=i;
            len[pos]=r[pos]-l[pos]+1;
        }
        n=pos;
        init();
        int x,y,xx,yy,ans1,ans2,ans3;
        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            xx=id[x];
            yy=id[y];
            if(xx==yy) ans1=ans2=y-x+1; //特殊情况、当x和y在同一个区间、答案是y-x+1
            else
            {
                ans1=r[xx]-x+1;
                ans2=y-l[yy]+1;
            }
            ans3=0;
            if(xx+1<=yy-1)ans3=query(xx+1,yy-1);
            printf("%d\n",max(max(ans1,ans2),ans3));
        }
    }
    return 0;
}
时间: 2024-10-09 17:25:44

RMQ [HDU 1806] Frequent values的相关文章

hdu 1806 Frequent values(给定一个非降序数组,求任意区间内出现次数最多的数的次数)

1.题目解析可见<训练指南>P198 2代码: #include<cstdio> #include<cstring> #include<cmath> #define Min(a,b) ((a)<(b)?(a):(b)) #define Max(a,b) ((a)>(b)?(a):(b)) #define N 100005 #define INF 1<<30 using namespace std; int a[N]; int valu

poj 1806 Frequent values(RMQ 统计次数) 详细讲解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1806 题目大意:给你一个非降序排列的整数数组,你的任务是对于一系列的询问,(i,j),回答序列中出现次数追的的数的个数: 如下图所示: AC代码: 1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 #include<queue> 6

Frequent values(HDU - 1806 )

Frequent values You are given a sequence of n integers a 1 , a 2 , ... , a n 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, determine the most frequent value

uva 11235 - Frequent values(RMQ)

题目链接:uva 11235 - Frequent values 题目大意:给定一个非降序的整数数组,要求计算对于一些询问(i,j),回答ai,ai+1,-,aj中出现最多的数出现的次数. 解题思路:因为序列为非降序的,所以相同的数字肯定是靠在一起的,所以用o(n)的方法处理处每段相同数字的区间.然后对于每次询问: num[i]=num[j]:j?i+1 numi≠numj:max(RMQ(righti+1,reftj?1),max(righti?i+1,j?leftj+1)) #include

POJ 3368 Frequent values RMQ ST算法/线段树

                                                     Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15229   Accepted: 5550 Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In

UVA 11235 Frequent values(RMQ)

Frequent values TimeLimit:3000Ms 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, determine the most f

POJ 3368 Frequent values (基础RMQ)

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

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