D. Frequent values

Time Limit: 3000ms
Case Time Limit: 3000ms
Memory Limit: 131072KB
 
64-bit integer IO format: %lld      Java class name: Main
 
2007/2008 ACM International Collegiate Programming Contest 
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的更多相关文章

  1. UVA - 11235 Frequent values

    2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...

  2. poj 3368 Frequent values(RMQ)

    /************************************************************ 题目: Frequent values(poj 3368) 链接: http ...

  3. H - Frequent values

    Problem F: Frequent values You are given a sequence of n integers a1 , a2 , ... , an in non-decreasi ...

  4. Frequent values && Ping pong

    Frequent values 题意是不同颜色区间首尾相接,询问一个区间内同色区间的最长长度. 网上流行的做法,包括翻出来之前POJ的代码也是RMQ做法,对于序列上的每个数,记录该数向左和向右延续的最 ...

  5. 【暑假】[实用数据结构]UVa11235 Frequent values

    UVa 11235 Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11241   Accep ...

  6. [HDU 1806] Frequent values

    Frequent values Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. 数据结构(RMQ):UVAoj 11235 Frequent values

    Frequent values You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. I ...

  8. [POJ] 3368 / [UVA] 11235 - Frequent values [ST算法]

    2007/2008 ACM International Collegiate Programming Contest University of Ulm Local Contest Problem F ...

  9. poj 3368 Frequent values(段树)

    Frequent values Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13516   Accepted: 4971 ...

  10. UVA 11235 Frequent values(RMQ)

    Frequent values TimeLimit:3000Ms , ... , an in non-decreasing order. In addition to that, you are gi ...

随机推荐

  1. WPF 控件树

    WPF控件树按照基类进行分类,记录下来便于编写自定义控件时查阅 RangeBase范围控件 Thumb拖到控件 TextBoxBase文本控件 ItemControl组控件 ContrentContr ...

  2. 前端之CSS常见兼容性问题

    1.双倍浮动BUG: 描述:块状元素设置了float属性后,又设置了横向的margin值,在IE6下显示的margin值要比设置的值大: 解决方案:给float的元素添加 display:inline ...

  3. 剑指tomcat之多项目部署问题

    部署项目时遇到的问题,tomcat的webapps文件夹中有两个war包,但每次启动Tomcat服务时,只会默认启动一个war包. 解决方案一:在Tomcat主页中进入应用管理页面,手动开启项目.(进 ...

  4. 【踩坑】Safari不兼容webpack封装的vue项目

    刚完成 Iblog 博客项目,在 chrome 浏览器调试完后,用 Safari 打开网站,页面一直停留在加载状态. 后来网上说这是 Safari 不支持 ES6 所致. 经过搜索,在 github ...

  5. 用户登陆界面(jquery)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. Ubuntu 16.04 not a com32r image

    安装Ubuntu16.04,出现题目中的错误,解决方法如下 重点:开机后按TAB键,在随后出现的命令行提示符中输入live 既可,之后的过程就是正常的过程了!

  7. 华硕笔记本刷BIOS

    笔记本硬件升级后想使用微软的Windows xp mode,之后发现笔记本BIOS中没有虚拟化选项,想通过升级BIOS的方法来解决,结果失败. 升级后出现关机后无法关闭电源指示灯以及风扇的问题,之后只 ...

  8. COGS 36. 求和问题

    时间限制:1.2 s   内存限制:128 MB [问题描述]     在一个长度为n的整数数列中取出连续的若干个数,并求它们的和. [输入格式]     输入由若干行组成,第一行有一个整数n    ...

  9. 允许Java App(applet)粘贴方法

    修改安全策略文件: "java.policy" JRE6的路径在:"C:\Program Files (x86)\Java\jre6\lib\security" ...

  10. Make 学习笔记(1)

    Make 学习笔记(1) 参考: GNU make 学习总结(1) 基础 make是帮助程序员使编译器明白如何编译工程的一种工具; 核心是规则. 规则一般由三部分组成: 目标(target) 必要条件 ...