【POJ】2823 Sliding Window
单调队列。
/* 2823 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = 1e6+;
int n, k;
int a[maxn];
int Q[maxn], P[maxn];
int mn[maxn], mx[maxn]; void getMin() {
int l = , r = ;
int i; for (i=; i<k-; ++i) {
while (l<=r && Q[r]>=a[i])
--r;
Q[++r] = a[i];
P[r] = i;
} for (; i<n; ++i) {
while (l<=r && Q[r]>=a[i])
--r;
Q[++r] = a[i];
P[r] = i;
while (P[l] < i-k+)
++l;
mn[i-k+] = Q[l];
}
} void getMax() {
int l = , r = ;
int i; for (i=; i<k-; ++i) {
while (l<=r && Q[r]<=a[i])
--r;
Q[++r] = a[i];
P[r] = i;
} for (; i<n; ++i) {
while (l<=r && Q[r]<=a[i])
--r;
Q[++r] = a[i];
P[r] = i;
while (P[l] < i-k+)
++l;
mx[i-k+] = Q[l];
}
} void solve() {
getMin();
getMax();
rep(i, , n-k+) {
if (i == n-k)
printf("%d\n", mn[i]);
else
printf("%d ", mn[i]);
}
rep(i, , n-k+) {
if (i == n-k)
printf("%d\n", mx[i]);
else
printf("%d ", mx[i]);
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif scanf("%d %d", &n, &k);
rep(i, , n)
scanf("%d", &a[i]);
solve(); #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【POJ】2823 Sliding Window的更多相关文章
- 【原创】leetCodeOj --- Sliding Window Maximum 解题报告
天,这题我已经没有底气高呼“水”了... 题目的地址: https://leetcode.com/problems/sliding-window-maximum/ 题目内容: Given an arr ...
- POJ 题目2823 Sliding Window(RMQ,固定区间长度)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 46507 Accepted: 13442 ...
- 【LeetCode】239. Sliding Window Maximum
Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving fr ...
- 【LeetCode】239. Sliding Window Maximum 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调递减队列 MultiSet 日期 题目地址:ht ...
- 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】1704 Georgia and Bob(Staircase Nim)
Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
随机推荐
- C# ACM poj1002
排序 public static void acm1002(string[] azx) { string[] a = new string[azx.Length]; ; i < azx.Leng ...
- NodeJS连接MongoDB数据库时报错
今天第一次尝试连接MongoDB数据库,具体步骤也很简单. 首先,通过NodeJS运行环境安装MongoDB包,进入要安装的目录,执行语句 npm install mongodb 安装成功后,通过如下 ...
- checkbox prop()函数
1.设置checkbox选中状态 ①选中: .prop('checked',true); ②不选中:.prop('checked',false); 2.获取checkbox选中状态 .prop('ch ...
- Famount game manufacuters
一.Valve「美国」 1.Valve「中文意:阀门」の来源 REFRE.Valve官网 REFER.a history of valve's opening logos REFER.theguyin ...
- 为UITextView添加与UITextField一样的边框——UITextField默认边框颜色、宽度、圆角
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3789052.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- TreeMap 红黑树实现
TreeMap 是一个有序的key-value集合,它是通过 红黑树 实现的. TreeMap 继承于AbstractMap,所以它是一个Map,即一个key-value集合. TreeMap 实现了 ...
- MySql启动提示:The server quit without updating PID file(…)失败
在网上找了很多 1.可能是/usr/local/mysql/data/rekfan.pid文件没有写的权限解决方法 :给予权限,执行 “chown -R mysql:mysql /var/data” ...
- cocos2d-x学习笔记------动画人物跑起来吧!
学习总结: 1.sprintf用来格式化字符串 2.CCSpriteFrame:: frameWithTexture通过图片名创建的时候需要的参数Texture2D创建使用CCTextureCache ...
- MySQL基础学习之数据表
查看数据表 SHOW TABLE; 查看数据表详细结构 SHOW CREATE TABLE 表名\G; 创建数据表 CREATE TABLE 表名(数据名 类型,数据名1 类型2); CREATE ...
- Project not selected to build for this solution configuration.
Project not selected to build for this solution configuration. When you upgrade your older solutio ...