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. python搭建ftp服务器

    1 # coding: utf-8 import os from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handler ...

  2. 安装MySQLdb出现HAVE_WCSCOLL重定义问题的解决方法

    root@wodeyitian MySQL-python-1.2.3]# python setup.py install running install running bdist_egg runni ...

  3. 清理ThreadLocal

    在我很多的课程里(master.concurrency.xj-conc-j8),我经常提起ThreadLocal.它经常受到我严厉的指责要尽可能的避免使用.ThreadLocal是为了那些使用完就销毁 ...

  4. 洛谷 P2362 围栏木桩

    题目描述 某农场有一个由按编号排列的n根木桩构成的首尾不相连的围栏.现要在这个围栏中选取一些木桩,按照原有的编号次序排列之后,这些木桩高度成一个升序序列.所谓的升序序列就是序列中的任何一个数都不小于它 ...

  5. mongo ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

    解决方法 rm /var/lib/mongodb/mongod.lock

  6. UVA 11400 Lighting System Design 照明系统设计

    首先是一个贪心,一种灯泡要么全都换,要么全都不换. 先排序,定义状态d[i]为前面i种灯泡的最小花费,状态转移就是从d[j],j<i,加上 i前面的j+1到i-1种灯泡换成i的花费. 下标排序玩 ...

  7. java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName'

    java.sql.SQLException: Incorrect string value: '\xE6\x88\x91\xE7\x9A\x84...' for column 'groupName' ...

  8. Vue-Quill-Editor 富文本编辑器的使用

    步骤如下: 1.下载Vue-Quill-Editor npm install vue-quill-editor --save 2.下载quill(Vue-Quill-Editor需要依赖) npm i ...

  9. chosen选择框加载数据

    1.单选$(select).val($("#id").val());$(select).trigger("chosen:updated"); 2.多选 func ...

  10. hibernate4整合spring3.1的过程中的异常问题

    (1)hibernate4整合spring3.1的过程中,发现了java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider异常 ...