poj 3368 Frequent values(RMQ)
题目:http://poj.org/problem?id=3368
题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数。。
大白书上的 例题。。算是RMQ变形了,
对 原数组重新分段,并标记相同的个数为 该段的数值,然后RMQ...
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <algorithm>
- using namespace std;
- const int maxn = ;
- const int maxm = ;
- int d_max[maxn][maxm],a[maxn];
- int n,t;
- int val[maxn],cnt[maxn],num[maxn],l[maxn],r[maxn];
- void RMQ_init()
- {
- int i,j;
- memset(d_max,,sizeof(d_max));
- for(i = ; i <= t; i++)
- {
- d_max[i][] = cnt[i];
- }
- for(j = ; (<<j) <= t; j++)
- for(i = ; i + j - <= t; i++)
- {
- d_max[i][j] = max(d_max[i][j-], d_max[i + (<<(j-))][j-]);
- }
- }
- int RMQ_max(int l, int r)
- {
- if(l>r)
- return ;
- int k = ;
- while((<<(k+)) <= r-l+)
- k++;
- return max(d_max[l][k], d_max[r-(<<k)+][k]);
- }
- int main()
- {
- int i, j, le, rig;
- int q, ans, k, maxs;
- while(~scanf("%d",&n) && n)
- {
- scanf("%d",&q);
- for(i = ; i <= n; i++)
- {
- scanf("%d",&a[i]);
- }
- i = ;
- t = ;
- while(i <= n)
- {
- j = i;
- ans = ;
- while(a[j] == a[i] && j <=n)
- {
- j++;
- ans++;
- }
- for(k = i; k < j; k++)
- {
- num[k] = t; //位置k的编号
- l[k] = i; //位置k的最左端编号
- r[k] = j-; //位置k的最右端编号
- }
- val[t] = a[i]; //第i段的数值
- cnt[t] = ans; //第i段的出现次数
- t++; i = j;
- }
- RMQ_init();
- while(q--)
- {
- scanf("%d%d",&le, &rig);
- if(num[le] == num[rig])
- maxs = rig - le +;
- else
- {
- maxs = max(r[le] - le + , rig - l[rig] + );
- maxs = max(maxs, RMQ_max(num[le]+, num[rig]-));
- }
- printf("%d\n",maxs);
- }
- }
- return ;
- }
poj 3368 Frequent values(RMQ)的更多相关文章
- poj 3368 Frequent values(段树)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13516 Accepted: 4971 ...
- POJ 3368 Frequent values (基础RMQ)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14742 Accepted: 5354 ...
- UVA 11235 Frequent values(RMQ)
Frequent values TimeLimit:3000Ms , ... , an in non-decreasing order. In addition to that, you are gi ...
- UVA-11235 Frequent values (RMQ)
题目大意:在一个长度为n的不降序列中,有m次询问,每次询问(i,j)表示在区间(i,j)中找出出现次数最多的元素的出现次数. 题目分析:因为序列有序,可以将序列分段,并且记录每段的元素个数.每一个元素 ...
- 【POJ 3368】Frequent values(RMQ)
Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...
- poj 1806 Frequent values(RMQ 统计次数) 详细讲解
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1806 题目大意:给你一个非降序排列的整数数组,你的任务是对于一系列的询问,(i,j),回答序列中出现次 ...
- POJ 3368 Frequent values(线段树区间合并)
[题目链接] http://poj.org/problem?id=3368 [题目大意] 有一个有序序列,要求区间查询出现次数最多的数 [题解] 维护每个区间左端点和右端点,以及左右的长度,还有区间的 ...
- (简单) POJ 3368 Frequent values,RMQ。
Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...
- POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】
传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS Memory Limit: 65536K Total S ...
随机推荐
- 【经验】Angularjs 中使用 layDate 日期控件
layDate 控件地址:http://laydate.layui.com/ 前情:原来系统中使用的日期控件是UI bootstrap(地址:https://angular-ui.github.io/ ...
- Java 集合类(一)
今天我们先讲一下Collection: Collection和Collections的区别: java.util.Collection是一种java集合接口,它提供了对集合对象的基本操作通用接口方法, ...
- BZOJ 1854: [Scoi2010]游戏 无向图判环
题目链接: 题目 1854: [Scoi2010]游戏 Time Limit: 5 Sec Memory Limit: 162 MB 问题描述 lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装 ...
- HDU 5637 Transform 单源最短路
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5637 题意: http://bestcoder.hdu.edu.cn/contests/contes ...
- 【转载】struct和typedef struct彻底明白了
分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int a; }Stu; 于是在声明变量的时候就可 ...
- iOS开发之数据存取3-CoreData自定义数据类型
当系统提供的类型不能达到我们的使用要求时,比如我想在CoreData中存储UIColor,该怎么办呢? 这时候就要用到CoreData中非常强大的一个存储类型了:Transformable 下面将通过 ...
- 转载一个不错的Scrapy学习博客笔记
背景: 最近在学习网络爬虫Scrapy,官网是 http://scrapy.org 官方描述:Scrapy is a fast high-level screen scraping and web c ...
- Ignore files which are already versioned
If you accidentally added some files which should have been ignored, how do you get them out of vers ...
- 浏览器后退按钮导致jquery动态添加的select option值丢失的解决方法
监控浏览器返回功能 判断浏览器返回功能 禁用浏览器的后退按钮 JS禁止浏览器后退键 http://volunteer521.iteye.com/blog/830522/ 浏览器返回功能 判断上一页面来 ...
- 利用vim阅读源代码一个好用的工具
阅读源代码时常常遇到找变量,函数定义的问题.vim为我们提供了一个好用的工具,ctags. 安装 ctags. 在 libvirt的源代码根目录运行 ctags -R . vim -t virConn ...