(POJ3368)Frequent values RMQ 求区间出现次数最多的数出现的次数

2024-02-05 02:32

本文主要是介绍(POJ3368)Frequent values RMQ 求区间出现次数最多的数出现的次数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Frequent values
Description

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

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

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
Source

Ulm Local 2007

题意:
给你一个大小为n的非递减整型数组,有q次查询,问[L,R]中出现次数最多的数出现的次数

分析:“来自白书第三章”
由于是非递减的,所以相同的数会在一起出现,我们可以从左到右按数值将数组分段。count[i]表示第i段的数出现的次数,num[p],left[p],right[p]分别表示位置p所在的段的编号和左右端点的位置。
每次查询[L,R]的结果为一下3个部分的最大值:从L到L所在段的结束处的元素的个数即 right[L] - L + 1,从R所在段的开始处到R处的元素的个数即 R - lfet[R] +1 ,中间第num[L]+1段到第num[R]-1段的count的最大值。
特殊情况: 如果L,R在同一段,则答案为R-L+1

AC代码:

// UVa11235 Frequent Values
// Rujia Liu
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;const int maxn = 100000 + 5;
const int maxlog = 20;// 区间最*大*值
struct RMQ {int d[maxn][maxlog];void init(const vector<int>& A) {int n = A.size();for(int i = 0; i < n; i++) d[i][0] = A[i];for(int j = 1; (1<<j) <= n; j++)for(int i = 0; i + (1<<j) - 1 < n; i++)d[i][j] = max(d[i][j-1], d[i + (1<<(j-1))][j-1]);}int query(int L, int R) {int k = 0;while((1<<(k+1)) <= R-L+1) k++; // 如果2^(k+1)<=R-L+1,那么k还可以加1return max(d[L][k], d[R-(1<<k)+1][k]);}
};int a[maxn], num[maxn], left[maxn], right[maxn];
RMQ rmq;
int main() {int n, q;while(scanf("%d%d", &n, &q) == 2) {for(int i = 0; i < n; i++) scanf("%d", &a[i]);a[n] = a[n-1] + 1; // 哨兵int start = -1;vector<int> count;for(int i = 0; i <= n; i++) {if(i == 0 || a[i] > a[i-1]) { // 新段开始if(i > 0) {count.push_back(i - start);for(int j = start; j < i; j++) {num[j] = count.size() - 1; left[j] = start; right[j] = i-1;}}start = i;}}rmq.init(count);while(q--) {int L, R, ans;scanf("%d%d", &L, &R); L--; R--;if(num[L] == num[R]) ans = R-L+1;else {ans = max(R-left[R]+1, right[L]-L+1);if(num[L]+1 < num[R]) ans = max(ans, rmq.query(num[L]+1, num[R]-1));}printf("%d\n", ans);}}return 0;
}

这篇关于(POJ3368)Frequent values RMQ 求区间出现次数最多的数出现的次数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/679518

相关文章

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

PTA求一批整数中出现最多的个位数字

作者 徐镜春 单位 浙江大学 给定一批整数,分析每个整数的每一位数字,求出现次数最多的个位数字。例如给定3个整数1234、2345、3456,其中出现最多次数的数字是3和4,均出现了3次。 输入格式: 输入在第1行中给出正整数N(≤1000),在第二行中给出N个不超过整型范围的非负整数,数字间以空格分隔。 输出格式: 在一行中按格式“M: n1 n2 ...”输出,其中M是最大次数,n

hdu 3065 AC自动机 匹配串编号以及出现次数

题意: 仍旧是天朝语题。 Input 第一行,一个整数N(1<=N<=1000),表示病毒特征码的个数。 接下来N行,每行表示一个病毒特征码,特征码字符串长度在1—50之间,并且只包含“英文大写字符”。任意两个病毒特征码,不会完全相同。 在这之后一行,表示“万恶之源”网站源码,源码字符串长度在2000000之内。字符串中字符都是ASCII码可见字符(不包括回车)。

hdu4267区间统计

题意:给一些数,有两种操作,一种是在[a,b] 区间内,对(i - a)% k == 0 的加value,另一种操作是询问某个位置的值。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import

hdu4417区间统计

给你一个数列{An},然后有m次查询,每次查询一段区间 [l,r] <= h 的值的个数。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamRead

hdu3333区间统计

题目大意:求一个区间内不重复数字的和,例如1 1 1 3,区间[1,4]的和为4。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;

C语言练习题之 数组中出现次数超过一半的数

题目描述 给一个长度为 n 的数组,数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。 例如输入一个长度为9的数组[1,2,3,2,2,2,5,4,2]。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。 数据范围:n≤50000,数组中元素的值0≤val≤10000 要求:空间复杂度:O(1),时间复杂度O(n) 输入描述: 保证数组输入非空,且保证有

【uva】11536-Smallest Sub-Array(区间移动问题)

一个区间移动的问题,1A了,感觉没什么好说的。。 13975926 11536 Smallest Sub-Array Accepted C++ 0.809 2014-08-01 11:00:20 #include<cstdio>#include<cstring>#include<iostream>using namespace std;#define INF 1 << 30

【hdu】Just a Hook(线段树区间修改)

线段树模板题,练的是懒惰标记。 懒惰标记,就是更新一段区间的时候,如果小区间被包含在了所需要更新的区间里面,那么直接对代表这个区间的数组元素赋值,之后做一个标记(表示这个区间的子区间都需要更新)但是不继续递归(这样可以节省很多的时候)。 116571152014-09-15 14:17:26Accepted1698796MS2380K1750 BG++KinderRiven #

【hdu】I Hate It(线段树,结点修改求区间最大值)

线段树的模板题,还是二分递归。 #include <iostream>#include <cstdlib>#include <cstdio>#include <string>#include <cstring>#include <cmath>#include <vector>#include <queue>#include <set>#include <map>#incl