UVA 11235 Frequent values 非递减序列 l r范围内 出现最多的数字次数 RMQ
2013-06-26 17:49
459 查看
Frequent values
[Submit] [Go
Back] [Status]
Description
2007/2008 ACM International Collegiate Programming Contest
University of Ulm Local Contest
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.
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.
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所在的段的连续个数的最大值
具体看代码
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
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). Thenext 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; }
相关文章推荐
- POJ 3368 Frequent values(RMQ 区间出现频率最多数字次数)
- POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)
- POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)
- 【POJ 3368】【RMQ 或者 线段树】Frequent values【求出区间内连续出现次数最多的数的次数。】
- UVA - 11235 Frequent values 频繁出现的数值(RMQ)
- 随机生成50个数,范围在【10,50】,统计每个数字出现的次数以及出现次数最多的数字与它的个数。
- POJ 3368—— Frequent values(频繁出现的数值UVA11235) RMQ
- UVA 11235 RMQ(范围最小值(最大值)问题):Sprase Table算法
- 一个数组arr,长度为N,N小于10^8,里面数字的范围是1~N,求每个数字出现的次数
- Python检测手机号出现次数最多的数字
- 统计你的手机号码中出现次数最多的数字,并打印出此数字及其出现次数
- uva 11235 - Frequent values(RMQ问题)
- uva 11235 - Frequent values(RMQ)
- 由自然数1-1000中某些数字,找出出现次数最多的数字
- 求整数序列中出现次数最多的数
- 1.12.在序列中查找出现次数最多的元素
- UVA 1449 Dominating Patterns (LA4670) 出现次数最多的子串 ac自动机
- UVA11235 Frequent values(RMQ)
- 给你一个整型数组如{1,3,4,7,2,1,1,5,2}, * 打印出现次数最多的那个数,如果最多的次数相同,则打印数字大的那个数。
- python 找出序列中出现次数最多的元素方法。