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 among the integers a i , ... , a j .

InputThe 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.

OutputFor 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! 

题意:查询区间[l,r]中众数的个数.

因为数列是非递减的,所以可以转化为ST表.

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define N 100005
using namespace std;
int maxn[N][64],a[N],pre[N];
int n,q,i,j;
int main()
{
    while(scanf("%d",&n)!=EOF&&n)
    {
        scanf("%d",&q);
        memset(maxn,0,sizeof maxn);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            if(i==1)
            {
                pre[i]=1;
                continue;
            }
            if(a[i]==a[i-1])
            pre[i]=pre[i-1]+1;
            else
            pre[i]=1;
        }
        for(i=1;i<=n;i++)
        maxn[i][0]=pre[i];
        int t=log2(n);
        for(j=1;j<=t;j++)
        {
            for(i=1;i+(1<<j)-1<=n;i++)
            {
                maxn[i][j]=max(maxn[i][j-1],maxn[i+(1<<(j-1))][j-1]);
            }
        }
        while(q--)
        {
            int MAX;
            int l,r;
            scanf("%d%d",&l,&r);
            int m=l;
            while(m<=r&&a[m]==a[m-1])//区间第一个数字要特判.
            m++;
            if(r<m)
            MAX=0;
            else
            {
                int k=log(r-m+1)/log(2);
                MAX=max(maxn[m][k],maxn[r-(1<<k)+1][k]);
            }
            MAX=max(MAX,m-l);
            printf("%d\n",MAX);
        }
    }
} 

原文地址:https://www.cnblogs.com/switch-waht/p/11396907.html

时间: 2024-08-14 23:03:45

Frequent values(HDU - 1806 )的相关文章

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-decreasin

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

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(线段树解法)

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

POJ3368 Frequent values 线段树

N个数为非递减顺序,给定范围l,r,求[l,r]区间内数字出现频率最高的次数. 可以用线段树来做.先说查询,我们设节点P对应的区间为[a, b],左孩子节点为p1,右孩子节点为p2,那么 P也许不等于 max(p1 , p2),原因是如果p1中频率较低的某个数与p2中出现频率较低的某个数是同一个数,并且两者出现次数加起来大于max(p1, p2),但是,题目说N个数为非递减顺序排列,所以这个可能只会发生在p1的右端和p2的左端对应的数是同一个数,因此,我们还要处理这第三个可能. 查询的问题解决,

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 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

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/ 这题就是套模板的,纪念

UVA 11235 Frequent values

1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int maxn=100005; 7 8 int count[maxn]; 9 int num[maxn],ll[maxn],rr[maxn]; 10 int tot; 11 int d[maxn][50]; 12 13 int rmq (int l,int r){ 14 int