[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
The last test case is followed by a line containing a single 0.
-1 -1 1 1 1 1 3 10 10 10
2 3
1 10
5 10
0
4
3
A naive algorithm may not run in time!
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define N 100010 int n,m;
int a[N];
int dp[N][];
int id[N];
int len[N],l[N],r[N]; void init()
{
int i,j;
for(i=;i<=n;i++)
{
dp[i][]=len[i];
}
int k=(int)(log((double)n)/log(2.0));
for(j=;j<=k;j++)
{
for(i=;i+(<<j)-<=n;i++)
{
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
}
}
}
int query(int i,int j)
{
int k=(int)(log((double)(j-i+))/log(2.0));
int res=max(dp[i][k],dp[j-(<<k)+][k]);
return res;
}
int main()
{
int i,pos;
while(scanf("%d",&n),n)
{
pos=;
scanf("%d",&m);
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]!=a[i-])
{
pos++;
l[pos]=i;
}
id[i]=pos;
r[pos]=i;
len[pos]=r[pos]-l[pos]+;
}
n=pos;
init();
int x,y,xx,yy,ans1,ans2,ans3;
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
xx=id[x];
yy=id[y];
if(xx==yy) ans1=ans2=y-x+; //特殊情况、当x和y在同一个区间、答案是y-x+1
else
{
ans1=r[xx]-x+;
ans2=y-l[yy]+;
}
ans3=;
if(xx+<=yy-)ans3=query(xx+,yy-);
printf("%d\n",max(max(ans1,ans2),ans3));
}
}
return ;
}
[HDU 1806] Frequent values的更多相关文章
- hdu 1806 Frequent values 线段树
题目链接 给一个非递减数列, n个数, m个询问, 每个询问给出区间[L, R], 求这个区间里面出现次数最多的数的次数. 非递减数列, 这是最关键的一个条件... 需要保存一个区间最左边的数, 最右 ...
- poj 1806 Frequent values(RMQ 统计次数) 详细讲解
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1806 题目大意:给你一个非降序排列的整数数组,你的任务是对于一系列的询问,(i,j),回答序列中出现次 ...
- 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 ...
- H - Frequent values
Problem F: Frequent values You are given a sequence of n integers a1 , a2 , ... , an in non-decreasi ...
- 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 ...
- 数据结构(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 / [UVA] 11235 - Frequent values [ST算法]
2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...
随机推荐
- 九度OJ 1207 质因数的个数
题目地址:http://ac.jobdu.com/problem.php?pid=1207 题目描述: 求正整数N(N>1)的质因数的个数. 相同的质因数需要重复计算.如120=2*2*2*3* ...
- C#拓展练习之模拟键盘录入
摘自<31天学会CRM项目开发<C#编程入门级项目实战>> 使用C#调用Windows API使程序模拟键盘输入,也可模拟按下快捷键.本例中,单击“模拟输入”按钮,可录入字符“ ...
- linux shell编程学习笔记(一)---通配符,元字符
linux通配符: 通配符是由shell处理的(不是由所涉及到命令语句处理的,其实我们在shell各个命令中也没有发现有这些通配符介绍), 它只会出现在 命令的“参数”里(它不用在 命令名称里, 也不 ...
- install xdebug on fedora
Compiling There is a wizard available that provides you with the correct file to download, and which ...
- Java security MD5加密算法
利用java.security对字符串进行MD5加密: import java.security.MessageDigest; import java.security.NoSuchAlgorithm ...
- C#基础(三)—重载与覆盖
所谓重载指的是同一个类中有两个或多个名字相同但是参数不同的方法.重载,必然发生在一个类中,函数名相同,参数类型或者顺序不同构成重载,与返回类型无关. override:过载也称重写是指子类对父类中虚函 ...
- 手机的touch事件(基于jquery)
javascript代码: $.swipe=function(opt){ var o = $.extend({ mainSelector:"", swipeLe ...
- org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode
[spring]:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowe ...
- 关于appStore评分的相关说明--转自张诚教授
在iOS7以前,评分地址如下 itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?typ ...
- 【经验】angularjs 实现带查找筛选功能的select下拉框
一.背景 对于select的下拉列表,像国家选择这样的功能,全世界那么多国家,一直拉滚动条多辛苦,眼睛也要盯着找,累!so,为优化用户体验,带查找功能的下拉框是非常非常有必要的.都知道jquery里有 ...