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 ...
随机推荐
- Less的学习和使用
官网 http://less.bootcss.com/usage/ 在线编译器 http://tool.oschina.net/less
- kafka java api生产者
import java.util.HashMap; import java.util.List;import java.util.Map;import java.util.Properties; im ...
- 集合框架比较两个对象是否相同(equals和hashCode方法)
package com.dcz.hashset; import java.util.HashSet; import java.util.Set; /** * HashSet是接口最常用的实现类,顾名思 ...
- 2566. [51nod 1129] 字符串最大值
[题目描述] 一个字符串的前缀是指包含该字符第一个字母的连续子串,例如:abcd的所有前缀为a, ab, abc, abcd. 给出一个字符串S,求其所有前缀中,字符长度与出现次数的乘积的最大值. 例 ...
- 将centos6的php5.3升级为5.6
在阿里云主机上,操作系统是Centos6,php版本是5.3.因为安装Yii2.0的需要,我要升级php为5.4.因为还有5.5和5.6,当然要升到高版本了.我决定升到5.6. 首先,按照这里的步 ...
- win7 x64和win10 x64 windbg 本地调试记录
今天看CSDN和某文档看了win7 64位 和win10 64位 的windbg本地调试内核的办法 win7 x64 Step1:kdbgctrl –db Step2:kdbgctrl –e Step ...
- codevs 2059 逃出克隆岛
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description oi小组的yh酷爱玩魔兽rpg,每天都会在u9搜索最新的rpg地图. 今天,他 ...
- 洛谷 P2936 [USACO09JAN]全流Total Flow
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- PCL点云处理可视化——法向显示错误“no override found for vtk actor”解决方法
转:https://blog.csdn.net/bflong/article/details/79137692 参照:https://blog.csdn.net/imsaws/article/deta ...
- root.sh脚本支持checkpoints文件实现重复运行
安装集群GRID/GI一般包括三个过程:首先,运行OUI/RunInstaller输入集群配置信息,其次,拷贝/编译集群文件,最后,以root用户运行root.sh脚本配置集群/启动集群,其中运行ro ...