POJ 2823 Sliding Window ST RMQ
Description
The array is [1 3 -1 -3 5 3 6 7], and k is 3.
| Window position | Minimum value | Maximum value |
|---|---|---|
| [1 3 -1] -3 5 3 6 7 | -1 | 3 |
| 1 [3 -1 -3] 5 3 6 7 | -3 | 3 |
| 1 3 [-1 -3 5] 3 6 7 | -3 | 5 |
| 1 3 -1 [-3 5 3] 6 7 | -3 | 5 |
| 1 3 -1 -3 [5 3 6] 7 | 3 | 6 |
| 1 3 -1 -3 5 [3 6 7] | 3 | 7 |
Your task is to determine the maximum and minimum values in the sliding window at each position.
Input
Output
Sample Input
8 3
1 3 -1 -3 5 3 6 7
Sample Output
-1 -3 -3 -3 3 3
3 3 5 5 6 7
Source
#include <stdio.h>
#include <algorithm>
//#define LOG[i] = (i & (i - 1)) ? LOG[i - 1] : LOG[i - 1] + 1
#define MAXX 1234567
#include <vector>
using namespace std; int a[MAXX];
int dp1[MAXX][];
int LOG[MAXX]; void init(int n)
{
LOG[] = ;
for(int i=; i<=n; i++)
LOG[i]=(i&(i-))?LOG[i-]:LOG[i-]+;
} int ST(int l, int r, int i)
{
int k=LOG[r-l+];
if(i==)
return max(dp1[l][k],dp1[r-(<<k)+][k]);
if(i==)
return min(dp1[l][k],dp1[r-(<<k)+][k]);
}
int main()
{
int n, k; while(~scanf("%d%d",&n, &k))
{
int i, j;
init(n);
for(i=; i<=n; i++)
{
scanf("%d", &a[i]);
dp1[i][]=a[i];
}
for(j=; j<=; j++)
{
for(i=; i<=n; i++)
{
if(i+(<<j)->n)
break;
dp1[i][j]=min(dp1[i][j-], dp1[i+(<<(j-))][j-]);
}
}
for(i=; i<=n-k+; i++)
{
if(i!=)
printf(" ");
printf("%d", ST(i,i+k-,));
}
////// for(i=; i<=n; i++)
{
dp1[i][]=a[i];
for(j=; j<=; j++)
dp1[i][j]=;
}
for(j=; j<=; j++)
{
for(i=; i<=n; i++)
{
if(i+(<<j)->n)
break;
dp1[i][j]=max(dp1[i][j-], dp1[i+(<<(j-))][j-]);
}
}
printf("\n");
for(i=; i<=n-k+; i++)
{
if(i!=)
printf(" ");
printf("%d", ST(i,i+k-,));
}
printf("\n");
}
}
POJ 2823 Sliding Window ST RMQ的更多相关文章
- POJ 2823 Sliding Window + 单调队列
一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1) 从队首删除 (2) 从队尾删除 (3) 从队尾插入 (4) ...
- POJ 2823 Sliding Window 题解
POJ 2823 Sliding Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...
- 洛谷P1886 滑动窗口(POJ.2823 Sliding Window)(区间最值)
To 洛谷.1886 滑动窗口 To POJ.2823 Sliding Window 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每 ...
- POJ 题目2823 Sliding Window(RMQ,固定区间长度)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 46507 Accepted: 13442 ...
- poj 2823 Sliding Window (单调队列入门)
/***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...
- POJ 2823 Sliding Window(单调队列入门题)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 67218 Accepted: 190 ...
- POJ 2823 Sliding Window & Luogu P1886 滑动窗口
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 66613 Accepted: 18914 ...
- POJ 2823 Sliding Window
Sliding Window Time Limit: 12000MSMemory Limit: 65536K Case Time Limit: 5000MS Description An array ...
- POJ - 2823 Sliding Window (滑动窗口入门)
An array of size n ≤ 10 6 is given to you. There is a sliding window of size kwhich is moving from t ...
随机推荐
- 基础数据类型-tuple
Python中,元组tuple与list类似,不同之处在于tuple的元素不能修改,tuple使用(),list使用[], (1)元组的创建使用(),需要注意的是创建包含一个元素的元组: tuple_ ...
- HDU 4617 Weapon(三维几何)
Problem Description Doctor D. are researching for a horrific weapon. The muzzle of the weapon is a c ...
- Notes of the scrum meeting(12.12)
meeting time:19:30~20:30p.m.,December 12th,2013 meeting place:3号公寓一层 attendees: 顾育豪 ...
- 福大软工1816 · 第五次作业 - 结对作业2_map与unordered map的比较测试
测试代码: #include <iostream> using namespace std; #include <string> #include <windows.h& ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset Trie
题目链接: http://codeforces.com/contest/706/problem/D D. Vasiliy's Multiset time limit per test:4 second ...
- LintCode-69.二叉树的层次遍历
二叉树的层次遍历 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 返回他的分层遍历结果: [ [3], [9,2 ...
- ZOJ 1913 J-Eucild's Game
https://vjudge.net/contest/67836#problem/J Two players, Stan and Ollie, play, starting with two natu ...
- 某一线互联网公司前端面试题总结css部分
1,css3选择器 :not(selector) 选择页面内所有type!=text的类型: input:not([type=text]){ color: red; font-weight: bold ...
- QObject类 moc处理后代码
QObject在QT中是所有类的基类,经过MOC处理后代码如下 之所以贴出这段代码,是因为很多流程追踪到最后一些关键性函数都是出自这个类 源码 4.8.6 MOC版本 63 /************ ...
- HDU 3879 Base Station(最大权闭合子图)
将第i个用户和他需要的基站连边,转化成求二分图的最大权闭合子图. 答案=正权点之和-最小割. # include <cstdio> # include <cstring> # ...