H - Frequent values
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 RMQ问题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <cstdlib>
#include <sstream>
using namespace std;
typedef long long LL;
const int INF=0x5fffffff;
const double EXP=1e-;
const int MS=; int dp[MS][];
int a[MS],cnt[MS];
int num[MS],l[MS],r[MS];
int n,q; void RMQ_init()
{
for(int i=;i<n;i++)
dp[i][]=cnt[i];
for(int j=;(<<j)<=n;j++)
{
for(int i=;i+(<<j)-<n;i++)
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
}
} int RMQ(int l,int r)
{
int k=;
while(<<(k+)<=r-l+)
k++;
return max(dp[l][k],dp[r-(<<k)+][k]);
} int main()
{
while(scanf("%d%d",&n,&q)==&&n)
{
for(int i=;i<n;i++)
scanf("%d",&a[i]);
a[n]=a[n-]+;
int start=;
int id=;
for(int i=;i<=n;i++)
{
if(i>&&a[i]>a[i-])
{
for(int j=start;j<i;j++)
r[j]=i-;
cnt[id]=i-start;
id++;
start=i;
}
l[i]=start;
num[i]=id;
}
n=id;
RMQ_init();
int x,y;
while(q--)
{
scanf("%d%d",&x,&y);
x--;
y--;
int ans=;
if(num[x]==num[y])
{
printf("%d\n",y-x+);
continue;
}
ans=max(r[x]-x+,y-l[y]+);
if(num[x]+<num[y])
ans=max(ans,RMQ(num[x]+,num[y]-));
printf("%d\n",ans);
} }
return ;
}
H - Frequent values的更多相关文章
- [POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]
2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...
- POJ 3368:Frequent values
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14764 Accepted: 5361 ...
- UVA - 11235 Frequent values
2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...
- poj 3368 Frequent values(RMQ)
/************************************************************ 题目: Frequent values(poj 3368) 链接: http ...
- Frequent values && Ping pong
Frequent values 题意是不同颜色区间首尾相接,询问一个区间内同色区间的最长长度. 网上流行的做法,包括翻出来之前POJ的代码也是RMQ做法,对于序列上的每个数,记录该数向左和向右延续的最 ...
- 【暑假】[实用数据结构]UVa11235 Frequent values
UVa 11235 Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11241 Accep ...
- [HDU 1806] Frequent values
Frequent values Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 数据结构(RMQ):UVAoj 11235 Frequent values
Frequent values You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. I ...
- poj 3368 Frequent values(段树)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13516 Accepted: 4971 ...
随机推荐
- 第三百三十四天 how can I 坚持
I give up my dream that day,else,I coming on,the day my heart is die…… 那天,梦已碎,那天,心已死. 晚上看了个电影<奔爱& ...
- Delphi Refactor 重构
delphi refactor procedure TCameraComponentForm.btnRefreshConfigClick(Sender: TObject); var a:string; ...
- http://blogs.msdn.com/b/pranavwagh/archive/2007/03/03/word-2007-file-seems-to-be-deleted-when-you-open-and-save-it-using-dsoframer.aspx
http://blogs.msdn.com/b/pranavwagh/archive/2007/03/03/word-2007-file-seems-to-be-deleted-when-you-op ...
- [转]模拟HttpContext 实现ASP.NET MVC 的单元测试
众所周知 ASP.NET MVC 的一个显著优势即可以很方便的实现单元测试,但在我们测试过程中经常要用到HttpContext,而默认情况下单元测试框架是不提供HttpContext的模拟的,本文通过 ...
- [转]undefined reference问题总结
转自http://ticktick.blog.51cto.com/823160/431329 最近在Linux下编程发现一个诡异的现象,就是在链接一个静态库的时候总是报错,类似下面这样的错误: (.t ...
- LC串联谐振回路
- c++中的array数组和vector数组
我觉得实验一下会记得比较牢,话不多直接上代码. 下面是array数组,感觉用的不多. //cpp 风格数组 array #include <iostream> #include <a ...
- 监控SQL
http://www.cnblogs.com/downmoon/archive/2009/08/12/1544764.html
- [翻译]比较ADO.NET中的不同数据访问技术(Performance Comparison:Data Access Techniques)
Performance Comparison: Data Access Techniques Priya DhawanMicrosoft Developer Network January 2002 ...
- 表单input按钮在各浏览器之间的兼容性
从网上看了这篇关于表单input按钮的浏览器兼容性问题,总结的还不错,所以copy下来学习下. input按钮在各个浏览器之间的兼容性问题,看下边这段代码: input.item { backgrou ...