题意:给出一个序列,求出每连续k个数字中最大的数和最小的数。

这是道单调队列裸题,直接写就行了。

本来用deque写出来后,发现在poj上硬是超时了,在discuss上看很多人也在抱怨超时的问题,据说在uestc上也有这题,我过去提交终于过了。。。

但是poj还是没有过,于是我用数组模拟队列来写,提交还是超时,折腾了一会,把g++改成c++终于5s多过了。。。

注意如果是直接输出答案的话,如果k=1可能会出错。

代码:

#include <cstdio>
#include <cstring>
#include <deque>
using namespace std; const int MAXN = 1000005;
int n, k, t, arr[MAXN], que[MAXN]; void solve(bool flag) {
int head(0), rear(0);
memset(que, 0, sizeof(que));
if (k == 1) printf("%d ", arr[0]);
for (int i = 1; i < n; i++) {
if (flag) while (head <= rear && arr[i] > arr[que[rear]])
rear--;
else while (head <= rear && arr[i] < arr[que[rear]])
rear--;
que[++rear] = i;
if (que[head] < i - k + 1) head++;
if (i >= k - 1)
if (i == n - 1) printf("%d\n", arr[que[head]]);
else printf("%d ", arr[que[head]]);
}
} int main() {
while (scanf("%d%d", &n, &k) != EOF) {
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
solve(0);
solve(1);
}
return 0;
}

POJ 2823 UESTCoj 1221 Sliding Window 单调队列 经典入门题的更多相关文章

  1. POJ 2823 Sliding Window + 单调队列

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

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

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

  3. POJ 2823:Sliding Window 单调队列

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 48930   Accepted: 14130 ...

  4. POJ 2823 Sliding Window (单调队列)

    单调队列 加了读入挂比不加更慢.... 而且这份代码要交c++ 有大神G++跑了700ms..... orzorzorz #include<iostream> #include<cs ...

  5. POJ2823 Sliding Window(单调队列)

    单调队列,我用deque维护.这道题不难写,我第二次写单调队列,1次AC. -------------------------------------------------------------- ...

  6. poj 2823 Sliding Windows (单调队列+输入输出挂)

    Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 73426   Accepted: 20849 ...

  7. 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 ...

  8. poj2823Sliding Window——单调队列

    题目:http://poj.org/problem?id=2823 单调队列模板. 代码如下: #include<iostream> #include<cstdio> usin ...

  9. poj 3017 Cut the Sequence(单调队列优化 )

    题目链接:http://poj.org/problem?id=3017 题意:给你一个长度为n的数列,要求把这个数列划分为任意块,每块的元素和小于m,使得所有块的最大值的和最小 分析:这题很快就能想到 ...

随机推荐

  1. bzoj 1143: [CTSC2008]祭祀river / 2718: [Violet 4]毕业旅行 -- 二分图匹配

    1143: [CTSC2008]祭祀river Time Limit: 10 Sec  Memory Limit: 162 MB Description 在遥远的东方,有一个神秘的民族,自称Y族.他们 ...

  2. 01-项目简介Springboot简介入门配置项目准备

    总体课程主要分为4个阶段课程: ------------------------课程介绍------------------------ 01-项目简介Springboot简介入门配置项目准备02-M ...

  3. centos安装pcre

    安装pcre前需要已安装gcc工具 1.跳转下载目录 cd install-file 2.下载pcre wget ftp://ftp.csx.cam.ac.uk/pub/software/progra ...

  4. HTTP Error 404.2 - Not Found The page you are requesting cannot be served because of the ISAPI and CGI Restriction list settings on the Web server(转)

    今天公司的同事問我,為什麼同一支程式在自己的電腦OK,部署到Server上會出現下面的錯誤 我想,沒有錯啊~ 我在這台Server所部署的程式一向都是OK的 看了錯誤的Error page, 發現是I ...

  5. fdopen()和fileno()函数

    转:http://book.2cto.com/201212/11763.html 文件描述字函数是流函数的初等函数,每一个流都与一个描述字相连.给定一个打开的文件描述字,可以用fdopen()函数为它 ...

  6. DevExpress Winform 通用控件打印方法(允许可自定义边距) z

    DevExpress Winform 通用控件打印方法,包括gridcontrol,treelist,pivotGridControl,ChartControl,LayoutControl...(所有 ...

  7. Oracle经典查询案例

    1.创建一个学生管理数据库,名称为student 2.最少包括一下三张表 S (SNO,SNAME)学生关系.SNO 为学号,SNAME 为姓名 C (CNO,CNAME,CTEACHER)  课程关 ...

  8. ASPNET CORE初探

    ASP.NET Core 开发-中间件(Middleware)   ASP.NET Core开发,开发并使用中间件(Middleware). 中间件是被组装成一个应用程序管道来处理请求和响应的软件组件 ...

  9. linux ifconfig

    Linux and Unix ifconfig command Quick links About ifconfig Syntax Examples Related commands Linux an ...

  10. PHP:面向对象学习笔记,重点模拟Mixin(掺入)

    背景 相对于Python.Node和Ruby来说PHP算是一门容易学习和使用的语言,因为这个特点也使其成为WEB开发领域的佼佼者,本文记录一下我对PHP面向对象部分的学习笔记. 先来一个复杂的例子:M ...