D. Frequent values
D. Frequent values
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). 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 解题:注意处理边界,我的d存出现次数,是从左往右的,故左边界很需要处理!而右边界就不需要处理了。因为左边界的左边一个元素正是我们选取区间的最优值可能算入了边界左边的与边界相等的数值。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
const int maxn = ;
int d[maxn],dt[maxn];
struct node {
int lt,rt,val,index;
} tree[maxn<<];
void build(int lt,int rt,int v) {
tree[v].lt = lt;
tree[v].rt = rt;
if(lt == rt) {
tree[v].val = d[lt];
tree[v].index = lt;
return;
}
int mid = (lt+rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
if(tree[v<<].val >= tree[v<<|].val) {
tree[v].val = tree[v<<].val;
tree[v].index = tree[v<<].index;
} else {
tree[v].val = tree[v<<|].val;
tree[v].index = tree[v<<|].index;
}
tree[v].val = max(tree[v<<].val,tree[v<<|].val);
}
int query(int &index,int lt,int rt,int v) {
if(tree[v].lt == lt && tree[v].rt == rt) {
index = tree[v].index;
return tree[v].val;
};
int mid = (tree[v].lt+tree[v].rt)>>;
int a,b,x,y;
if(rt <= mid) return query(index,lt,rt,v<<);
else if(lt > mid) return query(index,lt,rt,v<<|);
else {
x = query(a,lt,mid,v<<);
y = query(b,mid+,rt,v<<|);
if(x >= y) {
index = a;
return x;
} else {
index = b;
return y;
}
}
}
int main() {
int n,m,i,x,y,temp,a,b,index,j;
while(scanf("%d",&n),n) {
scanf("%d",&m);
for(i = ; i <= n; i++)
scanf("%d",dt+i);
temp = d[] = ;
for(i = ; i <= n; i++) {
if(dt[i] == dt[i-]) temp++;
else temp = ;
d[i] = temp;
}
build(,n,);
for(i = ; i < m; i++) {
scanf("%d %d",&x,&y);
temp = query(index,x,y,);
if(dt[x] == dt[y]) printf("%d\n",y-x+);
else if(x == || dt[index] != dt[x-]) printf("%d\n",temp);
else {
for(j = x+; dt[j] == dt[j-] && j <= y; j++);
temp = query(index,j,y,);
printf("%d\n",max(j-x,temp));
}
}
}
return ;
}
RMQ
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int a[maxn],b[maxn],c[maxn][],n,m,s,t;
int main() {
while(scanf("%d",&n),n){
scanf("%d",&m);
for(int i = ; i < n; i++)
scanf("%d",b+i);
int tmp = ;
a[n-] = tmp;
for(int i = n-; i >= ; i--){
if(b[i] == b[i+]) ++tmp;
else tmp = ;
a[i] = tmp;
}
for(int i = n-; i >= ; --i){
c[i][] = a[i];
for(int j = ; i + (<<j) <= n; ++j)
c[i][j] = max(c[i][j-],c[i+(<<(j-))][j-]);
}
while(m--){
scanf("%d %d",&s,&t);
--s;
--t;
tmp = lower_bound(b+s,b+t+,b[t]) - b;
int ans = t - tmp + ;
t = tmp - ;
int r = log2(t - s + 1.0);
if(t <= s) printf("%d\n",ans);
else printf("%d\n",max(ans,max(c[s][r],c[t-(<<r)+][r])));
}
}
return ;
}
D. Frequent values的更多相关文章
- 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 ...
- [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 / [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: 13516 Accepted: 4971 ...
- UVA 11235 Frequent values(RMQ)
Frequent values TimeLimit:3000Ms , ... , an in non-decreasing order. In addition to that, you are gi ...
随机推荐
- arcengine,c# 二次开发
plication.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); ESRI.ArcGIS.Ru ...
- 【转】阐述Handler的实现原理
面试题:阐述Handler的实现原理 2016年07月18日 21:01:35 阅读数:7574 处理过程: 从handler中获取一个消息对象,把数据封装到消息对象中,通过handler的send… ...
- sublime text less安装踩坑图文讲解(less无法生成css)
唉,怎么感觉做个前端几乎把所有的坑都踩遍了啊,别人按照网上安装了一遍就好使,我这里就死活不行. 先说一下我的问题:网上说的能安装的都按了,可是sublime就是不给我生成css文件,后来知道了,就是l ...
- ios UITableView顶部向下偏移
//设置向下偏移20[self.tableView setContentInset:UIEdgeInsetsMake(20,0,0,0)];
- 关于Retrofit + RxJava 的使用
年前一个月到现在,一直都在忙一个项目.项目使用的三方框架还是蛮多的. 下面来总结一下自己使用Retrofit + RxJava的知识点吧. (以下讲述从一个请求的最初开始) 1.首先定义一个RxMan ...
- uvm_sequence_item——sequence机制(一)
让子弹飞一会 UVM框架,将验证平台和激励分开,env以下属于平台部分,test和sequence属于激励,这样各司其职.我们可以将sequence_item 比喻成子弹,sequencer 类比成弹 ...
- codevs 1313 质因数分解
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题目描述 Description 已知正整数 n是两个不同的质数的乘积,试求出较大的那个质数 . 输入描述 I ...
- maven打包错误:No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[INFO] Scanning for projects...[INFO] ...
- elasticsearch最全详细使用教程:入门、索引管理、映射详解、索引别名、分词器、文档管理、路由、搜索详解
一.快速入门1. 查看集群的健康状况http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 状 ...
- Java poi 的使用
poi可操作老旧版本的excel 下载jar包,http://archive.apache.org/dist/poi/release/bin/poi-bin-3.17-20170915.tar.gz ...