http://poj.org/problem?id=2823

【题意】

  • 给定一个长度为n的序列,求长度为k的滑窗内的最大值和最小值

【思路】

  • 裸的单调队列
  • 注意用C++提交,不然会T,orz我用G++T了好长时间

【AC】

 //#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
int n,k;
const int maxn=1e6+;
int a[maxn];
int mx[maxn];
int mn[maxn];
struct node
{
int x;
int pos;
}q[maxn];
void solve()
{
//mx
int head=;int tail=;
for(int i=;i<=n;i++)
{
while(tail>=head&&q[tail].x<=a[i]) tail--;
q[++tail].x=a[i];q[tail].pos=i;
while(q[head].pos<=i-k) head++;
if(i>=k) mx[i]=q[head].x;
}
//mn
head=;tail=;
for(int i=;i<=n;i++)
{
while(tail>=head&&q[tail].x>=a[i]) tail--;
q[++tail].x=a[i];q[tail].pos=i;
while(q[head].pos<=i-k) head++;
if(i>=k) mn[i]=q[head].x;
}
for(int i=k;i<=n;i++)
{
if(i!=n) printf("%d ",mn[i]);
else printf("%d",mn[i]);
}
puts("");
for(int i=k;i<=n;i++)
{
if(i!=n) printf("%d ",mx[i]);
else printf("%d",mx[i]);
}
puts("");
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
solve();
}
return ;
}

单调队列

【单调队列】poj 2823 Sliding Window的更多相关文章

  1. POJ 2823 Sliding Window + 单调队列

    一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1)   从队首删除 (2)   从队尾删除 (3)   从队尾插入 (4)   ...

  2. POJ 2823 Sliding Window 题解

    POJ 2823 Sliding  Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...

  3. 洛谷P1886 滑动窗口(POJ.2823 Sliding Window)(区间最值)

    To 洛谷.1886 滑动窗口 To POJ.2823 Sliding Window 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每 ...

  4. poj 2823 Sliding Window (单调队列入门)

    /***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...

  5. POJ 2823 Sliding Window(单调队列入门题)

      Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 67218   Accepted: 190 ...

  6. POJ 2823 Sliding Window 【单调队列】

    题目链接:http://poj.org/problem?id=2823 题目大意:给出一组数,一个固定大小的窗体在这个数组上滑动,要求出每次滑动该窗体内的最大值和最小值. 这就是典型的单调队列,单调队 ...

  7. 题解报告:poj 2823 Sliding Window(单调队列)

    Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...

  8. POJ 2823 Sliding Window (线段树/单调队列)

    题目不说了,可以用线段树或者单调队列,下面附上代码. 线段树: #include <iostream> #include <stdio.h> #include <algo ...

  9. poj 2823 Sliding Window(单调队列)

    /* 裸地单调队列.. 第一次写 写的好丑.... */ #include<iostream> #include<cstdio> #include<cstring> ...

随机推荐

  1. http协议参数详解

    整理一下http协议中的一些参数详解 截取了一个当前项目中的请求作为示例: Genaral:通用头 Request URL:当前请求的请求地址 Request Method:请求类型 get.post ...

  2. php 正则符号说明

    preg_match_all ("/<b>(.*)<\/b>/U", $userinfo, $pat_array); preg_match_all (&qu ...

  3. nginx 编译某个模板的问题./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library stati

    root@hett-PowerEdge-T30:/usr/local/src/nginx-1.9.8# ./configure --prefix=/usr/local/nginx  --add-mod ...

  4. IOS修改系统音量

    #import <IOKit/IOKitLib.h> #import <IOKit/hidsystem/IOHIDLib.h> #import <IOKit/hidsys ...

  5. 用NSCoding协议完成“编码/解码”操作-Object-C

    Archiving Objective-C Objects with NSCoding For the seasoned Cocoa developer, this is a piece of cak ...

  6. 初涉二维数点问题&&bzoj1935: [Shoi2007]Tree 园丁的烦恼

    离线好评 Description 很久很久以前,在遥远的大陆上有一个美丽的国家.统治着这个美丽国家的国王是一个园艺爱好者,在他的皇家花园里种植着各种奇花异草.有一天国王漫步在花园里,若有所思,他问一个 ...

  7. java获取本地计算机MAC地址

    java获取本地计算机MAC地址代码如下: public class SocketMac { //将读取的计算机MAC地址字节转化为字符串 public static String transByte ...

  8. dom4j 常用操作

    package com.wanbang.wbyyb.common.util; import com.alibaba.fastjson.JSONObject; import com.wanbang.wb ...

  9. (转)automaticallyAdjustsScrollViewInsets(个人认为iOS7中略坑爹的属性)

    转自http://m.blog.csdn.net/blog/humingtao2013/27662093 automaticallyAdjustsScrollViewInsets(个人认为iOS7中略 ...

  10. 20130829ios cocos2d下拉列表的向上弹出实现(ios开发遇到的frame的问题)

    前几天仔细区分了ios中frame,bounds,center之间的关系. Frame:边框矩形,是视图相对于其父坐标的位置和大小 Bounds:边界矩形,是本地坐标系统(一般较少使用) Center ...