POJ_3368_Frequent values
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 19998 | Accepted: 7180 |
Description
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
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
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
Source
- 题目中序列是非递增序列,这样有序的序列有利于我们减少搜查的数据量
- 我们可以利用相同数都在一起的性质给序列分块
- 然后对于一个询问,我们总是整块地查询
- 就是说对于一个查询我们先得出最左边分块出现次数和最右边分块出现次数,然后中间剩余分块可以用ST表RMQ查询
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e6 + ;
const int inf = 0x3f3f3f3f ;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; int fac[], dp[maxn][];
int l[maxn], r[maxn];
void ST(int n){
int k=(int)(log((double)n)/log(2.0));
for(int i=;i<=n;i++)
dp[i][]=r[i]-l[i]+;
for(int j=;j<=k;j++)
for(int i=;i+fac[j]-<=n;i++)
dp[i][j]=max(dp[i][j-],dp[i+fac[j-]][j-]);
}
int RMQ(int u, int v){
int k=(int)(log((double)(v-u+))/log(2.0));
return max(dp[u][k],dp[v-fac[k]+][k]);
}
int n, m, u, v, ans, tot, a[maxn], b[maxn];
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
for(int i=;i<;i++)
fac[i]=(<<i);
while(~scanf("%d",&n)){
if(==n){break;}
scanf("%d",&m);
a[]=1e6+;
tot=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i-]!=a[i]){
tot++;
b[i]=tot;
l[tot]=i;
r[tot]=i;
}else{
b[i]=tot;
r[tot]++;
}
// b[i]=tot;
}
ST(tot);
while(m--){
scanf("%d %d",&u,&v);
if(b[u]==b[v]){
ans=v-u+;
}else if(b[u]+==b[v]){
ans=max(r[b[u]]-u+,v-l[b[v]]+);
}else{
ans=max(r[b[u]]-u+,v-l[b[v]]+);
ans=max(ans,RMQ(b[u]+,b[v]-));
}
printf("%d\n",ans);
}
}
return ;
}
POJ_3368_Frequent values的更多相关文章
- 全部省市县数据库(MySQL脚本) (转)
/*MySQL - 5.5.47 *************//*!40101 SET NAMES utf8 */; create table `base_area` ( `codeid` me ...
- JS+MySQL获取 京东 省市区 地区
采集了一下JD的省市区地区 (非常简单,只是做个记录) 1.建表:account_area 2.进入页面: https://reg.jd.com/reg/company 在浏览器(Firefox) ...
- salesforce 零基础学习(六十二)获取sObject中类型为Picklist的field values(含record type)
本篇引用以下三个链接: http://www.tgerm.com/2012/01/recordtype-specific-picklist-values.html?m=1 https://github ...
- Django models .all .values .values_list 几种数据查询结果的对比
Django models .all .values .values_list 几种数据查询结果的对比
- UVA - 11235 Frequent values
2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...
- codeforces 360 E - The Values You Can Make
E - The Values You Can Make Description Pari wants to buy an expensive chocolate from Arya. She has ...
- Default Parameter Values in Python
Python’s handling of default parameter values is one of a few things that tends to trip up most new ...
- iOS开发之SQLite--C语言接口规范(四) —— Result Values From A Query
数据库的在上一篇博客中<SQLite之C语言接口规范(三)——Binding Values To Prepared Statements>用到了如何从查询结果中取出结果值.今天的博客就详细 ...
- mybatis报错invalid types () or values ()解决方法
原因: Pojo类User没提供无参数构造方法, 加上该构造方法后,问题解决 ### Cause: org.apache.ibatis.reflection.ReflectionException ...
随机推荐
- Java查找替换文本文件内容
文本替换几乎是所有文本编辑器都支持的功能,但是要限制在编辑其中才可以执行该功能.本实例实现了制定文本文件的内容替换,并且不需要再编辑其中打开文本文件. 思路: 先看视图层,要有一个JButton控件用 ...
- 关于MySQL数据库
MySQL最流行的关系型数据库管理系统 MySQL官网:http://www.mysql.com MySQL是WEB应用方面最好的RDBMS应用软件之一 RDBMS:Relational Databa ...
- Mac 使用 launchctl 定时运行程序(转载)
摘要: 在linux下可以用crontab来定时执行任务,在mac下可以用launchctl来定时执行任务. 在linux下可以用crontab来定时执行任务,在MAC下可以用launchctl来定时 ...
- java的代理和动态代理简单测试
什么叫代理与动态代理? 1.以买火车票多的生活实例说明. 因为天天调bug所以我没有时间去火车票,然后就给火车票代理商打电话订票,然后代理商就去火车站给我买票.就这么理解,需要我做的事情,代理商帮我办 ...
- Nginx 域名重定向
假设 www.old.com 为旧的域名,而 www.new.com 为新的域名,要实现当我们访问 new 的时候自动重定向到 old 域名,配置如下: server { //第一种配置方法 serv ...
- mybatis 之 parameterType="List" 2
<select id="queryGoodsByGoodsNoPcweb" parameterType="List" resultMap="si ...
- 给TextView加上多彩效果:改变部分字体的大小和颜色
转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/18363899 前言 在实际使用中,有时候会遇到特殊需求,比如pm突发奇想,想 ...
- C语言中打印返回值
demo: ----return :返回值------------------ int mosquitto_username_pw_set(struct mosquitto *mosq, const ...
- thinkjs中修改默认主键
报错信息: { Error: ER_BAD_FIELD_ERROR: Unknown column 'a_role.id' in 'field list' 还原场景: a_role这张表没有自增的id ...
- springaop---->springaop的使用(一)
与大多数技术一样, AOP 已经形成了自己的术语.描述切面的常用术语有通知(advice).切点(pointcut)和连接点(join point).从今天开始,我们对spring的切面编程做一个总结 ...