(简单) POJ 3368 Frequent values,RMQ。
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.
题目就是求区间范围内出现最高的频率是多少。
用RMQ的话,统计某个数和这个之前的频率,然后询问的时候就是求两部分的最大值,第一部分是与x相同的数的个数,第二部分是第一个与x不同到y的部分,用RMQ求第二部分。
然后无力吐槽自己,logN数组开小了,结果一直WA,真是够傻逼的。
代码如下:
// ━━━━━━神兽出没━━━━━━
// ┏┓ ┏┓
// ┏┛┻━━━━━━━┛┻┓
// ┃ ┃
// ┃ ━ ┃
// ████━████ ┃
// ┃ ┃
// ┃ ┻ ┃
// ┃ ┃
// ┗━┓ ┏━┛
// ┃ ┃
// ┃ ┃
// ┃ ┗━━━┓
// ┃ ┣┓
// ┃ ┏┛
// ┗┓┓┏━━━━━┳┓┏┛
// ┃┫┫ ┃┫┫
// ┗┻┛ ┗┻┛
//
// ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy
// Created Time : 2015年07月17日 星期五 17时11分45秒
// File Name : 3368.cpp #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MaxN=; int dp[MaxN][];
int logN[MaxN]; //!!! void init(int N,int num[])
{
logN[]=-; for(int i=;i<=N;++i)
{
logN[i]=logN[i-]+((i&(i-))==);
dp[i][]=num[i];
} for(int j=;j<=logN[N];++j)
for(int i=;i+(<<j)-<=N;++i)
dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
} int RMQ(int a,int b)
{
if(a>b)
return ; int k=logN[b-a+]; return max(dp[a][k],dp[b-(<<k)+][k]);
} int num[MaxN];
int rem[MaxN],wei[MaxN];
int N,Q; int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int a,b; while(~scanf("%d",&N) && N)
{
scanf("%d",&Q); rem[]=; for(int i=;i<=N;++i)
scanf("%d",&num[i]); for(int i=;i<=N;++i)
if(num[i]==num[i-])
rem[i]=rem[i-]+;
else
rem[i]=; wei[N]=N; for(int i=N-;i>=;--i)
if(num[i]==num[i+])
wei[i]=wei[i+];
else
wei[i]=i; init(N,rem); while(Q--)
{
scanf("%d %d",&a,&b);
printf("%d\n",max(RMQ(wei[a]+,b),min(wei[a],b)-a+));
}
} return ;
}
(简单) POJ 3368 Frequent values,RMQ。的更多相关文章
- poj 3368 Frequent values(RMQ)
/************************************************************ 题目: Frequent values(poj 3368) 链接: http ...
- 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变形了, 对 原数组重 ...
- [RMQ] [线段树] POJ 3368 Frequent Values
一句话,多次查询区间的众数的次数 注意多组数据!!!! RMQ方法: 预处理 i 及其之前相同的数的个数 再倒着预处理出 i 到不是与 a[i] 相等的位置之前的一个位置, 查询时分成相同的一段和不同 ...
随机推荐
- 建立TCP连接的三次握手
请求端(通常称为客户)发送一个 SYN 报文段( SYN 为 1 )指明客户打算连接的服务器的端口,以及初始顺序号( ISN ).服务器发回包含服务器的初始顺序号( ISN )的 SYN 报文段( S ...
- ant android打包--学习第一弹
1. 准备工作 用eclipse创建一个android项目 安装ant和SDK,并且添加到系统环境变量 2.ant 使用 2.1 ant简单的帮助命令 ant -p 2.2 创建ant配置文件%AND ...
- split和join函数的比较
关于split和join方法 处理对象字符串.split拆分字符串,join连接字符串 string.join(sep): 以string作为分隔符,将seq中的所有元素(字符串表示)合并成一个新的字 ...
- UIImageView 浅析
UIImageView summary UIImageView极其常用,功能比较专一:显示图片 pooperty @property(nonatomic,retain) UIImage *image; ...
- Spark集群搭建中的问题
参照<Spark实战高手之路>学习的,书籍电子版在51CTO网站 资料链接 Hadoop下载[链接](http://archive.apache.org/dist/hadoop/core/ ...
- linux服务器安装php GD扩展库方法
Strict Standards: Only variables should be assigned by reference in/home/wienholl/public_html/includ ...
- 关于MyEclipse不停报错multiple problems have occurred 或者是内存不足 的解决办法
这是因为 worksapace与svn代码不一样,要更新! 一更新就好了,困扰死我了,卧槽,搞了2个小时,难怪svn一提交就卡死人,原来还就是svn的问题,更新一下就行.
- 页面新宠图片格式WebP
WebP格式,谷歌(google)开发的一种旨在加快图片加载速度的图片格式.图片压缩体积大约只有JPEG的2/3,并能节省大量的服务器带宽资源和数据空间.Facebook Ebay等知名网站已经开始测 ...
- android脚步---图片浏览
简单的图片浏览器,实现图像显示与点击切换下一张 首先在main.xml里面定义一个简单的线性布局容器. <?xml version="1.0" encoding=" ...
- ZOJ3944People Counting<暴力/枚举>
题意:输入一张照片,给出人物的特征,判断有多少个人. .O. /|\ (.) 思路:按照3*3的图统计,只要有一个点符合就加1 #include<cstdio> #include<i ...