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 ...
随机推荐
- POJ3345 Bribing FIPA
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5021 Accepted: 1574 Description There ...
- Google解决跨域
1.添加 --disable-web-security --user-data-dir=D:\tmp 2.在D的根目录新建tmp文件夹
- 接阿里云oss有感
看API,从头细看到尾,在这个过程中一定会找到你要找的东西.
- T3186 队列练习2 codevs
http://codevs.cn/problem/3186/ 题目描述 Description (此题与队列练习1相比改了2处:1加强了数据 2不保证队空时不会出队)给定一个队列(初始为空),只有两种 ...
- Heavy Transportation(最短路)
poj 1797 ——Heavy Transportation 思路: 这道题我们可以采用类似于求最短路径的方法,用一种新的“松弛操作”去取代原本的方法. 我们可以记录d[u]为运送货物到点j时最大可 ...
- Camtasia Studio录制屏幕字迹不清晰的原因
Camtasia Studio这是一个很优秀的屏幕录像软件,功能强大且录制效果出色,支持众多格式输出: 之前一直用它录制视频的都很正常,但这次换系统后再重新安装后录制视频时,发现输出的视频画质不佳,文 ...
- java之 ------ 文件的输入、输出(一)
import java.io.*; public class IntFile { private String filename; public IntFile(String filename) { ...
- WSDL4J解析WSDL文件方法
利用wsdl4j解析WSDL文件 工具:wsdl4j1.6 解析wsdl文件是axis1.4的服务wsdl文件 wsdl文件: <?xml version="1.0" enc ...
- 使用UltraISO刻录自己的音乐CD步骤
1.文件->新建->音乐光盘映像. 2.在左下方,“本地目录”中,找到音乐所在目录,右下方会出现mp3等音乐文件. 3.在右下方,点击音乐文件,右键选“添加”.音乐文件会出现在右上方窗口里 ...
- C#连接数据库 增删改查