UVA11235 Frequent values
思路
连续的值只会分布在一起成一个块
讨论两边的块,中间就是RMQ了
ST表即可
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int times[100100],belong[100100],lb[100100],rb[100100],inq,n,q,st[100100][21];
void init_ST(void){
for(int i=1;i<=inq;i++)
st[i][0]=times[i];
for(int i=1;i<21;i++)
for(int j=1;j<=n;j++)
st[j][i]=max(st[j][i-1],st[min(j+(1<<(i-1)),n+10)][i-1]);
}
int query(int l,int r){
if(l>r)
return 0;
int k=0;
while((1<<(k+1))<=(r-l+1)){
k++;
}
return max(st[l][k],st[r-(1<<k)+1][k]);
}
int main(){
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
scanf("%d",&n);
while(n){
scanf("%d",&q);
memset(times,0,sizeof(times));
memset(belong,0,sizeof(belong));
memset(st,0,sizeof(st));
inq=0;
int last=0x3f3f3f3f;
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
if(x!=last){
last=x;
++inq;
times[inq]=1;
lb[inq]=i;
}
else
times[inq]++;
belong[i]=inq;
rb[inq]=i;
}
init_ST();
for(int i=1;i<=q;i++){
int l,r,lx,rx;
scanf("%d %d",&l,&r);
lx=belong[l];
rx=belong[r];
int lt=min(rb[lx],r)-max(lb[lx],l)+1,rt=min(rb[rx],r)-max(lb[rx],l)+1;
printf("%d\n",max(max(query(lx+1,rx-1),lt),rt));
}
scanf("%d",&n);
}
return 0;
}
UVA11235 Frequent values的更多相关文章
- 【暑假】[实用数据结构]UVa11235 Frequent values
UVa 11235 Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11241 Accep ...
- poj3368 uva11235 Frequent values
Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...
- UVA-11235 Frequent values (RMQ)
题目大意:在一个长度为n的不降序列中,有m次询问,每次询问(i,j)表示在区间(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做法,对于序列上的每个数,记录该数向左和向右延续的最 ...
- [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 ...
随机推荐
- cloud_note项目
导入mysql数据库: set names utf8; source cloud_note.sql 1.搭建springMvc+springIOc+Mybatis --引入jar包 ioc,aop,d ...
- spring 核心
1 Spring 1.1 专业术语了解 1.1.1 组件/框架设计 侵入式设计 引入了框架,对现有的类的结构有影响:即需要实现或继承某些特定类. 例如: Struts框架 非侵入式设计 引入了 ...
- STL之pair对组
#include<iostream> #include<algorithm> #include<cstring> #include<cstdlib> u ...
- pythonic operations
变量交换 >>> a, b = b, a 循环遍历区间元素 >>>for i in range(10): ... print (i) 返回的是生成器对象,生成器比列 ...
- [No0000198]swagger api一键导入postman
在用postman进行接口测试时,对于参数较多的接口时第一次添加接口参数是比较繁琐的,可利用swagger一键导入api接口,事例如下: 1.获取swagger地址 2.打开postman,点击imp ...
- python-----函数参数类型
#函数参数类型:1 位置参数 2 默认参数 3 关键字参数 4可变参数 包裹位置参数*args 包裹关键字参数 **kargs#参数位置顺序:先位置参数,默认参数,包裹位置,包裹关键字(定义和调用都应 ...
- Scaled Exponential Linear Unit
https://www.bilibili.com/video/av9770302/?p=11 Relu Leaky Relu Parametric Relu就是把leaky部分的斜率学出来,而不是指定 ...
- PHP中new self()和new static()的区别探究
1.new static()是在PHP5.3版本中引入的新特性. 2.无论是new static()还是new self(),都是new了一个新的对象. 3.这两个方法new出来的对象有什么区别呢,说 ...
- python队列基本使用
Python queue队列 作用: 解耦:使程序直接实现松耦合,修改一个函数,不会有串联关系. 提高处理效率:FIFO = 现进先出,LIFO = 后入先出. 队列: 队列可以并发的派多个线程, ...
- torch随机数 manual_seed
import torch seed = 2018 torch.manual_seed(seed) torch.cuda.manual_seed(seed) a=torch.rand([1,5]) # ...