poj 3368 Frequent values(RMQ)
- /************************************************************
- 题目: Frequent values(poj 3368)
- 链接: http://poj.org/problem?id=3368
- 题意: 给出n个数和Q个询问(l,r),对于每个询问求出(l,r)之
- 间连续出现次数最多的次数
- 算法: RMQ
- 思路: 借助数组f[i]。表示第i位前面有f[i]个相同的数。对于
- 每个区间(l,r)。暴力求前面几个相同的数。然后在用RMQ
- 求后面区间的值。
- *************************************************************/
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- #include<algorithm>
- #include<iostream>
- #include<cmath>
- using namespace std;
- const int mx=;
- int dp[mx][];
- int a[mx],f[mx];
- int n,q;
- void makermq()
- {
- for (int i=;i<=n;i++) dp[i][]=f[i];
- for (int j=;(<<j)<=n;j++)
- {
- for (int i=;i+(<<j)-<=n;i++)
- {
- dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
- }
- }
- }
- int rmq(int u,int v)
- {
- if (u>v) return ;
- int k=(int)(log(v-u+)/log(2.0));
- return max(dp[u][k],dp[v-(<<k)+][k]);
- }
- int main()
- {
- while (~scanf("%d",&n)&&n)
- {
- scanf("%d",&q);
- for (int i=;i<=n;i++) scanf("%d",&a[i]);
- f[]=;
- for (int i=;i<=n;i++)
- {
- if (a[i]==a[i-]) f[i]=f[i-]+;
- else f[i]=;
- }
- makermq();
- while (q--)
- {
- int l,r;
- scanf("%d%d",&l,&r);
- int ans=;
- for (l=l+;l<=r;l++)
- {
- if (a[l]!=a[l-]) break;
- ans++;
- }
- ans=max(ans,rmq(l,r));
- printf("%d\n",ans);
- }
- }
- }
poj 3368 Frequent values(RMQ)的更多相关文章
- POJ 3368 Frequent values RMQ ST算法/线段树
Frequent values Time Limit: 2000MS Memory Lim ...
- POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)
题目链接:http://poj.org/problem? id=3368 Description You are given a sequence of n integers a1 , a2 , .. ...
- POJ 3368 Frequent values RMQ 训练指南 好题
#include<cstdio> #include<cstring> ; const int inf=0x3f3f3f3f; inline int max(int x,int ...
- POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】
传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS Memory Limit: 65536K Total S ...
- POJ 3368 Frequent values (基础RMQ)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14742 Accepted: 5354 ...
- poj 3368 Frequent values(段树)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13516 Accepted: 4971 ...
- poj 3368 Frequent values(RMQ)
题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...
- (简单) POJ 3368 Frequent values,RMQ。
Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...
- [RMQ] [线段树] POJ 3368 Frequent Values
一句话,多次查询区间的众数的次数 注意多组数据!!!! RMQ方法: 预处理 i 及其之前相同的数的个数 再倒着预处理出 i 到不是与 a[i] 相等的位置之前的一个位置, 查询时分成相同的一段和不同 ...
随机推荐
- yii2 使用composer安装
composer global require "fxp/composer-asset-plugin:~1.0.0" composer create-project --prefe ...
- SQL 2000/2005/2008 收缩日志方法
一般情况下,SQL数据库的收缩并不能很大程度上减小数据库大小,其主要作用是收缩日志大小,应当定期进行此操作以免数据库日志过大. 方法一:清空日志.1.设置数据库模式为简单模式:打开SQL企业管理器,在 ...
- 21副GIF动图让你了解各种数学概念
baidu 21副GIF动图让你了解各种数学概念
- 通过List<String>动态传递参数给 sqlcommand.Parameters
通过List<String>动态传递参数 private void GetallChecked_TreeNote(TreeNodeCollection aNodes, ref int To ...
- MVC4.0网站发布和部署到IIS7.0上的方法
最近在研究MVC4,使用vs2010,开发的站点在发布和部署到iis7上的过程中遇到了很多问题,现在将解决的过程记录下来,以便日后参考,整个过程主要以截图形式呈现 vs2010的安装和mvc4的安装不 ...
- Unity5版本的AssetBundle打包方案之打包Scene场景
using UnityEngine; using System.Collections; using UnityEditor; /// <summary> /// 脚本位置:Editor文 ...
- [ActionScript 3.0] AS3.0 下雨及涟漪效果
帧代码: stage.frameRate = 80; function init(x1:Number,y1:Number) { var mc:MovieClip=new MovieClip(); ad ...
- tar打包排除某个目录
tar zcvf fd.tar.gz * --exclude=file1 --exclude=dir1 注意: 1.--exclude=file1 而不是 --exclude file1 2.要排除一 ...
- day6-2面向对象
概述: 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数进行分类和封装,让开发“更快更好更强...” 注:Java和C#来 ...
- Chap6: question 46 - 48
46. 求 1+2+ … +n. 要求:不用乘除法.for.while.if.else.switch.case 以及条件判断语句(A?B:C). a. 利用构造函数求解 #include <io ...