【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 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
随机推荐
- Ext.Net学习笔记23:Ext.Net TabPanel用法详解
Ext.Net学习笔记23:Ext.Net TabPanel用法详解 上面的图片中给出了TabPanel的一个效果图,我们来看一下代码: <ext:TabPanel runat="se ...
- 12天学好C语言——记录我的C语言学习之路(Day 12)
12天学好C语言--记录我的C语言学习之路 Day 12: 进入最后一天的学习,用这样一个程序来综合考量指针和字符串的关系,写完这个程序,你对字符串和指针的理解应该就不错了. //输入一个字符串,内有 ...
- Android笔记之adb命令应用实例1(手机端与PC端socket通讯上)
Android端的代码: 布局文件:activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/ ...
- From MSI to WiX, Part 2 - ARP support, by Alex Shevchuk
Following content is directly reprinted from From MSI to WiX, Part 2 - ARP support Author: Alex Shev ...
- 避免ajax请求过多,导致内存溢出,请求之后回收资源
php试题网 http://phpshiti.com/ http://www.jb51.net/article/30458.htm success: function (data, textStatu ...
- linux 配置Socks51
VPN大家耳熟能详,但是socks用到的人比较少,那什么是socks呢?请看第二段或者百度百科,socks分别有4和5两个版本,现在5为主流.工作中经常用VPN访问国外,但是同时国内的速度又慢了,让人 ...
- el-get
el-get Table of Contents 1. 依赖 2. 安装 3. 配置 3.1. 自定义包配置 4. 命令 5. 管理扩展 el-get 是一个emacs下的扩展管理工具.就像apt-g ...
- Java添加事件的四种方式
Java添加事件的几种方式(转载了codebrother的文章,做了稍微的改动) /** * Java事件监听处理——自身类实现ActionListener接口,作为事件监听器 * * @author ...
- windows 8.1 pro X64安装中断
用PE安装windows 8.1 pro X64 ISO镜像,快完成的时候卡在蓝色背景那不动了,等待了大概30min强制重启了. 奇怪的是,居然进去了,不过很慢.配置了一段时间终于看到桌面了,关机,失 ...
- Asp.net自带导出方法
///datatable数据源 filename绝对路径 如:E:\\***.xls DataTable.WriteXml(fileName)