Frequent values(POJ-1806)(RMQ)

4 篇文章 0 订阅

Problem Description
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 among the integers a i , ... , a j . 
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 a 1 , ... , a n(-100000 ≤ a i ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces. You can assume that for each i ∈ {1, ..., n-1}: a i ≤ a i+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

大意: 给定一个不递减长度为n的序列,给定m个询问,每次询问[li,ri]内上镜率最高的那个数的出现次数。

由于题中说明输入数据是一个非递减数列,可以知道相等的数字都集中在一起,然后可以将它们按出现的次数存入另一个数组运用RMQ求出区间出现的最大值,这时候要处理好左边界,由于数字如果相等则数组是从左往右递增的我们要处理好左边界。

例子:

输入的数组 :1 2 3 4 4 4 4 4 4 3 3 3 

转化后数组: 1 1 1 1 2 3 4 5 6 1 2 3

这个时候查询9-11这个区间  转化后的数组为 6 1 2,如果直接找最大值就是6,而正确答案是2.

所以要先从左边界开始找到一个不大于1的值并用sum记录下来,之后再用RMQ求最值与sum比较求得答案。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
typedef long long LL;
const int N=100099;
int dp[N][30],a[N],num[N];
void RMQ(int n)
{
    for(int i=1; i<=n; i++)
    {
        dp[i][0]=a[i];
    }

    for(int j=1; (1<<j)<=n; j++)
    {
        for(int i=1; (i+(1<<j)-1)<=n; i++)
        {
            dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
        }

    }
}
int main()
{
    int n,q;
    while(scanf("%d",&n)&&n)
    {
        memset(dp,0,sizeof(dp));
        memset(a,0,sizeof(a));
        scanf("%d",&q);
        for(int i=1; i<=n; i++)
            scanf("%d",&num[i]);
        //记录出现次数
        for(int i=1; i<=n; i++)
        {
            if(i==1)
            {
                a[i]=1;
                continue;
            }
            if(num[i]==num[i-1])
                a[i]=a[i-1]+1;
            else
                a[i]=1;
        }
        RMQ(n);
        while(q--)
        {
            int x,y,sum=0;
            scanf("%d%d",&x,&y);
            while(a[x]>1&&x<=y) //处理左侧边界 记录sum的值
            {
                if(a[x]>1)
                {
                    sum++;
                    x++;
                }
                else break;
            }
            if(x>=y&&sum==0) printf("1\n");//如果数组中出现 1 1这样的值
            else if(x>=y&&sum!=0) printf("%d\n",sum);//如果出现数组中7 1这样的值
            else
            {
                int k=log10(y-x+1)/log10(2);
                int mi=max(dp[x][k],dp[y-(1<<k)+1][k]);
                mi=max(mi,sum);
                printf("%d\n",mi);
            }
        }
    }

    return 0;



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值