您的位置:首页 > 产品设计 > UI/UE

UVA 11235 Frequent values 非递减序列 l r范围内 出现最多的数字次数 RMQ

2013-06-26 17:49 459 查看
Frequent values

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu
[Submit]   [Go
Back]   [Status]  

Description





2007/2008 ACM International Collegiate Programming Contest 

University of Ulm Local Contest

Problem F: Frequent values

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 Specification

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 Specification

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

A naive algorithm may not run in time!

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=23846

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2176

题意:

输入一个n个元素的非减序列a[],接着进行q次询问,每次询问输入两个数L, R, 问a[L]与a[R]之间相同元素的个数最多有多少个。

 

思路:  把每个相同的连续串合并到一起 分成多个段 段的数值一样   : 用(a,b)表示有b个连续的a     用num[i]  left[pos]     right[pos]   分别表示位置pos所在的段的编号 和左右端点位置  用count[i]表示第i段的出现次数

对于查询 l r    如果 lr再同一段 则结果为r-l+1  

如果不在同一段 则为求这中间多个段内的最大的count[i]值(RMQ)   以及l r所在的段的连续个数的最大值  

具体看代码

 

#include<stdio.h>
#include<string.h>
const int size=100111;
int value[size],count[size],num[size],left[size],right[size];
int d[size][50];
int mmax(int a,int b)
{
if(a>b) return a;
return b;
}
void  RMQ_init(int n)
{
int i,j;
for(i=1;i<=n;i++) d[i][0]=count[i];
for(j=1;(1<<j)<=n;j++)
for(i=1;i+j-1<=n;i++)
d[i][j]=mmax(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 mmax(d[l][k],d[r-(1<<k)+1][k]);
}
int main()
{
int n,m,i,j,k,last;
while(scanf("%d",&n)!=EOF)
{
if(!n) break;
scanf("%d",&m);
last=-99999999;
int id=0;//id表示段 有效id从1开始
int cnt=0;//cnt表示出现次数
int l=0,r=0;
for(i=1;i<=n;i++)
{
scanf("%d",&k);
if(k!=last)
{
r=i-1;
for(j=l;j<=r;j++)
{
num[j]=id;
left[j]=l;
right[j]=r;
}
if(cnt==0) cnt=1;
count[id]=cnt;
id++;
cnt=1;
l=i;
last=k;
}
else
{
cnt++;
last=k;
}
}
for(i=l;i<=n;i++)
{
num[i]=id;
left[i]=l;
right[i]=n;
}
count[id]=cnt;
//for(i=1;i<=n;i++)
//{
//printf("所在段num[i]=%d left[i]=%d right[i]=%d count[id]=%d\n"
//	,num[i],left[i],right[i],count[num[i]]);
//}

RMQ_init(id);
while(m--)
{
int mid,l,r,id1,id2,l_max,r_max,ans;
scanf("%d %d",&l,&r);
id1=num[l];id2=num[r];
if(id1==id2)
{
printf("%d\n",r-l+1);continue;
}
l_max=right[l]-l+1; r_max=r-left[r]+1;
ans=l_max>r_max?l_max:r_max;
if(id2-id1==1)
{
printf("%d\n",ans);
continue;
}
mid=RMQ(id1+1,id2-1);
ans=mmax(ans,mid);
printf("%d\n",ans);
}
}
return 0;
}


 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐