题目描述
题目链接←戳我
给出一个非递减数列,多次查询,每次查询一个区间内 出现次数最多的数 的出现次数(中文小心被绕进去,原题的英文还是很好理解的)
题目分析
应用分块的思想,把数字相同的分为一块,cnt[i]表示第i块中数字的个数,L[i]表示第i块的下标左边界,R[i]表示第i块的下标右边界(均为闭区间),bel[i]表示第i个数字属于第几块,使用ST表预处理区间cnt的最大值
-1 -1 1 1 1 1 3 10 10 10
cnt[times] 2 4 1 3
L[i] 1 3 7 8
R[i] 2 6 7 10
则区间[l,r]的答案为
R[bel[l]]-l+1
r-L[bel[r]]+1
第bel[l]+1块到bel[r]-1块的cnt[i]的最大值
这三者中的最大值

代码参考
/*
* @Author: CHAOS_ORDER
* @Date: 2019-09-14 14:58:21
* @LastEditors: CHAOS_ORDER
* @LastEditTime: 2019-09-17 16:43:21
* @Description: C - Frequent values http://acm.hdu.edu.cn/showproblem.php?pid=1806
* @Status: Accepted 280ms
*/
#include <functional>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define pdn(i) printf("%d\n", i)
#define plldn(i) printf("%lld\n", i)
#define pchstrn(i) printf("%s\n", i)
#

该博客介绍了如何解决UVA在线判题系统的11235题,即在非递减数列中查询区间内出现次数最多的数的出现次数。博主采用了分块思想和ST(区间最大值)表来优化算法,通过建立cnt数组记录每块中数字的个数,L和R数组分别表示块的左右边界,以及bel数组记录每个数字所属的块。通过预处理,可以快速计算出任何区间的答案,包括当前块的贡献和相邻块的贡献。
最低0.47元/天 解锁文章
664

被折叠的 条评论
为什么被折叠?



