POJ 3368 Frequent values

Frequent values
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15375 Accepted: 5592

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

Source

Ulm Local 2007

给定一个无序数列,相同的数在一起求a[i]到a[j]中重复次数最多数的个数。用dp[i][0]储存第i个位置与前面重复多少

如 -1 -1 1 1 1 1 3 10 10 10 对应是 1 2 3 1 2 3 4 1 1 2 3利用b[i]表示该位置上对应的数最后出现在数组中的位置如 -1 -1 1 1 1 1 3 10 10 10对应是 2 2 6 6 6 6 7 10 10 10;用rmq的st算法求最大值。可惜会错因为不能保证dp[i][0]是1所以会错

解决办法分两部搜索dp[i][0]到dp[z][0],dp[z+1][0]到dp[j][0](dp[z][0]中代表从i开始第一个值为1的下标)对于dp[z+1][0]到

dp[j][0]用rmq求其最大值即可,对于dp[i][0]到dp[z][0]可以知道他们的值都是相等的所以值为b[i]-i+1。

如果一开始b[i]>=j说明整个区间的值都一样输出j-i+1就好了。

ACcode:

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 100010
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
using namespace std;
int dp[maxn][30];
int a[maxn];
int b[maxn];
int t,x,y;
int mm[maxn];
int n,q;
void RMQ(){
    int t,k;
    for(int i=n;i>0;i--){
        if(i==n)
            t=n;
        else
            if(a[i]!=a[i+1])
                t=i;
        b[i]=t;
    }
    FOR(i,1,n){
        if(i==1)k=1;
        else {
            if(a[i]==a[i-1])k++;
            else k=1;
        }
        dp[i][0]=k;
    }
    FOR(j,1,20)
        FOR(i,1,n)
            if(i+(1<<j)-1<=n)
                     dp[i][j]=max(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
   /*FOR(i,1,n) cout<<b[i]<<" ";
    cout<<'\12';
    FOR(i,1,n) cout<<dp[i][0]<<" ";
    cout<<'\12';*/
}
void rmq(){
    if(b[x]>=y){
        printf("%d\n",y-x+1);
        return ;
    }
    int temp=x;
    x=b[x]+1;
    int k=mm[y-x+1];
    int ans=max(dp[x][k],dp[y-(1<<k)+1][k]);
  //  cout<<x<<"    "<<y;
    //cout<<"asdasd      "<<ans<<'\12';
    ans=max(ans,b[temp]-temp+1);
    printf("%d\n",ans);
}
int main(){
    while(rd(n)!=EOF&&n){
        MT(dp,0);
        MT(a,0);
        MT(b,0);
        rd(q);
        mm[0]=-1;
        FOR(i,1,n){
            rd(a[i]);
            mm[i]=(i&(i-1))?mm[i-1]:mm[i-1]+1;
        }
        RMQ();
        FOR(i,1,q){
            rd2(x,y);
            rmq();
        }
    }
    return 0;
}
/*
10 3
-1 -1 1 1 1 1 3 10 10 10
2 3
1 10
5 10
0
*/

错了好多次www




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值