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. 使用JDBC进行简单的增删改查

    JDBC为java的基础.用jdbc实现对数据库的增删改查的功能是程序员的基本要求.本例以mysql为例,首先要使用本例需要添加mysql-connector-java-5.1.7-bin.jar包. ...

  2. Azure powershell 获取 vmSize 可用列表的命令

    1.使用 Add-AzureAccount -Environment azurechinacloud 登录到订阅 2.选择默认的订阅 Select-AzureSubscription -Subscri ...

  3. Selenium私房菜系列9 -- Selenium RC服务器命令行参数列表【ZZ】

    本文转载自:http://wiki.javascud.org/display/SEL/Selenium+Remote+Control+-+options 使用示例: java -jar seleniu ...

  4. Azure 项目构建 – 托管静态网站

    本课程主要介绍了如何在 Azure 平台上快速构建和部署基于 Azure Web 应用的静态托管网站, 实践讲解如何使用 Azure 门户创建 Web 应用, 部署静态网站源代码,设置自定义域名等. ...

  5. get和post请求及进程和线程及cookie和session的区别

    get和post请求及进程和线程及cookie和session的区别 1.get和post请求的区别 get请求是指向服务器进行获取查询数据的请求,post请求指向服务器提交数据的请求. get请求如 ...

  6. sqlserver 视图用 case when

    视图用 case when 需要 用如下格式,[需要的列名]= case when...,而表里面的case 不用这样 [isNormal]=CASE WHENdbo.c_bdm_head.I_E_F ...

  7. Cordova插件中JavaScript代码与Java的交互细节介绍

    在Cordova官网中有这么一张架构图:大家看右下角蓝色的矩形框"Custom Plugin"--自定义插件.意思就是如果您用Cordova打包Mobile应用时,发现您的移动应用 ...

  8. 历史管理 onhashchange

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. GRANT - 定义访问权限

    SYNOPSIS GRANT { { SELECT | INSERT | UPDATE | DELETE | RULE | REFERENCES | TRIGGER } [,...] | ALL [ ...

  10. Matplotlib_常用图表

    Matplotlib绘图一般用于数据可视化 1.常用的图表有: 折线图(坐标系图) 散点图/气泡图 条形图/柱状图 饼图 直方图 箱线图 热力图 折线图(坐标系图) 折线图用于显示随时间或有序类别的变 ...