poj 3368 Frequent values 解题报告
题目链接:http://poj.org/problem?id=3368
题目意思:给出一段 n 个数的序列你,对于区间 [l, r] 的询问,找出 出现频率最高的数的次数。考虑到序列中的数是非递减的,也就是相同的数会连续不间断地在一起,于是就才有了代码中这个部分来预判了:
if (s > t)
printf("%d\n", ans);
这个人写RMQ 写得不错:http://dongxicheng.org/structure/lca-rmq/
这题就是套模板的,纪念第一个RMQ ^_^! 搞了整个晚上= =,懂了一点了,终于.......
详细请看代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; const int MAXN = 1e5 + ; int a[MAXN], b[MAXN];
int dp[MAXN][]; // dp[i][j] 实质对应别人的F[i, j] //dp[i][j]: 从 i 起连续2^j个数中的最大值
void makeRMQ(int n, int b[])
{
for (int i = ; i < n; i++)
{
dp[i][] = b[i];
// printf("dp[%d][0] = %d\n", i, dp[i][0]);
}
for (int j = ; (<<j) <= n; j++) // 2*j <= n
{
for (int i = ; i+(<<j)- < n; i++) // i+2*j-1 <= n
{
// printf("before: dp[%d][%d] = %d, dp[%d][%d] = %d\n", i, j-1, dp[i][j-1], i+(1<<(j-1)), j-1, dp[i+(1<<(j-1))][j-1]);
dp[i][j] = max(dp[i][j-], dp[i+(<<(j-))][j-]); // F[i, j]=max(F[i,j-1], F[i + 2^(j-1),j-1])
// printf("after: dp[%d][%d] = %d\n", i, j, dp[i][j]);
}
}
} int rmq(int s, int v)
{
int k = (int)(log(v-s+1.0) / log(2.0));
return max(dp[s][k], dp[v-(<<k)+][k]); // RMQ(A, i, j)=min{F[i,k],F[j-2^k+1,k]}
// k=[log2(j-i+1)]
} int find(int s, int t) // 找到序列a中(下标从0开始)和a[t](下标从1开始)值相同的第一个数(从左往右)的下标
{ // 例如对于 3 7, 返回的是6,因为a[7] = 3,和3相等的原始序列第一次出现3的时候下标是6(从0开始)
int tmp = a[t]; // 再例如对于2 6,返回的是2,因为a[6] = 1,第一次出现1的时候是下标2
int l = s;
int r = t;
while (l < r)
{
int mid = ((l+r)>>);
if (a[mid] >= tmp)
r = mid;
else
l = mid + ;
}
return r;
} int main()
{
int n, q, s, t;
while (scanf("%d", &n) != EOF && n)
{
scanf("%d", &q);
for (int i = ; i < n; i++)
scanf("%d", a+i);
int tmp;
for (int i = n-; i >= ; i--)
{
if (i == n-)
tmp = ;
else
{
if (a[i] == a[i+])
tmp++;
else
tmp = ;
}
b[i] = tmp; // 代表从后往前数,连续相同的数有多少个
}
makeRMQ(n, b);
while (q--)
{
scanf("%d%d", &s, &t);
s--, t--; // 下标从0开始
int tmp = find(s, t);
int ans = t - tmp + ;
t = tmp - ;
// printf("ans = %d, t = %d\n", ans, t);
if (s > t)
printf("%d\n", ans); // 这个区间只有连续的一段数,例如 [2, 6] 只有[1 1 1 1]
else
printf("%d\n", max(ans, rmq(s, t))); // 要从两段区间里找(详细看链接),因为这个区间有多个连续的一段数,例如[1, 10]有[1 1 1 1] 和[10 10 10]
}
}
return ;
}
poj 3368 Frequent values 解题报告的更多相关文章
- 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(poj 3368) 链接: http ...
- 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 ST算法/线段树
Frequent values Time Limit: 2000MS Memory Lim ...
- poj 3368 Frequent values -Sparse-Table
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16537 Accepted: 5981 Description You ...
- 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)
题目: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 ...
随机推荐
- 洛谷 [P2575] 高手过招
SG函数+状压记忆化搜索 观察题目发现,每一行都是独立的,只要处理出来每一行的SG值,异或起来就好 每一行的SG值可以用状压+记忆化搜索的方法来求,对位运算技术是个很大的考验 注意SG值要预处理出来, ...
- 对Netflix Ribbon的Loadbalancer类源码设计合理性的一点质疑
首先,这只是我个人的一点质疑,可能是因为我自己菜没有领悟到作者的意思,也正因此,想发出来跟大家一起探讨. 在昨晚,我因为在编写自己的开源项目的负载均衡模块(这是我开源项目的介绍:https://www ...
- POJ2167 Irrelevant Elements
Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Description Young cryp ...
- 【BZOJ1500】维修数列(splay)
题意: 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一条命令,格式参见问题描述中的 ...
- android图片上传
package com.example.center; import java.io.ByteArrayOutputStream;import java.io.InputStream; import ...
- 连接mysql报错 : The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone...
time zone 时区错误 DBEAVER连接MySQL运行报错The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or repres ...
- spring工具类获取bean
import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebAppl ...
- iOS 内存管理实践
内存管理实践 尽管基本的概念在内存管理策略文章中简单得阐述了,但是还有一些实用的步骤让你更容易管理内存:有助于确保你的程序最大限度地减少资源需求的同时,保持可靠和强大. 使用“访问器方法”让内存管理更 ...
- 干货--安装eclipse-hadoop-plugin插件及HDFS API编程两个遇到的重要错误的解决
在Windows的eclipse上写hdfs的API程序,都会遇到两个错误,在网上查了很多资料,都没有解决的办法,经过了很多时间的研究,终于把这个问题解决了 错误是 1.java.io.IOExcep ...
- Linux内核模块编程与内核模块LICENSE -《具体解释(第3版)》预读
Linux内核模块简单介绍 Linux内核的总体结构已经很庞大,而其包括的组件或许多.我们如何把须要的部分都包括在内核中呢?一种方法是把全部须要的功能都编译到Linux内核.这会导致两个问题.一是生成 ...