POJ 2823 Sliding Window 再探单调队列
重新刷这个经典题,感觉跟以前不一样了,变得更加容易理解了,不讲解了,看代码。注意:要用C++提交,用G++会超时。。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define N 1000007 int a[N],mp[N],head,tail,n,k; inline void pushup(int i)
{
while(tail > head && a[i] < a[mp[tail-]])
tail--;
mp[tail++] = i; //mq
} inline void pushdown(int i)
{
while(tail > head && a[i] > a[mp[tail-]])
tail--;
mp[tail++] = i;
} void solve(int cmd)
{
head = tail = ;
int i;
void (*push)(int) = cmd?pushdown:pushup;
for(i=;i<=k&&i<=n;i++)
push(i);
printf("%d",a[mp[head]]);
for(;i<=n;i++)
{
while(head != tail && mp[head] < i-k+)
head++;
push(i);
printf(" %d",a[mp[head]]);
}
printf("\n");
} int main()
{
int i;
while(scanf("%d%d",&n,&k)!=EOF)
{
for(i=;i<=n;i++)
scanf("%d",&a[i]);
solve();
solve();
}
return ;
}
POJ 2823 Sliding Window 再探单调队列的更多相关文章
- 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 (单调队列入门)
/***************************************************************** 题目: 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(单调队列)
Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...
- POJ 2823 Sliding Window 【单调队列】
题目链接:http://poj.org/problem?id=2823 题目大意:给出一组数,一个固定大小的窗体在这个数组上滑动,要求出每次滑动该窗体内的最大值和最小值. 这就是典型的单调队列,单调队 ...
- 【单调队列】poj 2823 Sliding Window
http://poj.org/problem?id=2823 [题意] 给定一个长度为n的序列,求长度为k的滑窗内的最大值和最小值 [思路] 裸的单调队列 注意用C++提交,不然会T,orz我用G++ ...
- POJ 2823 Sliding Window (线段树/单调队列)
题目不说了,可以用线段树或者单调队列,下面附上代码. 线段树: #include <iostream> #include <stdio.h> #include <algo ...
随机推荐
- Win7下安装依赖lxml的python插件
博主python菜鸟,本想在win7下安装一个pyquery玩玩爬虫,折腾了好几天终于搞好了,发现python这坑不是一般的深啊. 有一部分没有截图,请谅解 python版本3.4 1.下载easy_ ...
- Python for循环内部实现的一个sample
#!/usr/bin/env python # -*- coding: utf-8 -*- it = iter([1,2,3,4,5]) while True: try: x = next(it) p ...
- The main concepts
The MVC application model A Play application follows the MVC architectural pattern applied to the we ...
- sqlserver 存储过程 try catch TRANSACTION (转)
CREATE PROCEDURE YourProcedure ASBEGIN SET NOCOUNT ON; BEGIN TRY---------------------开始捕捉异常 ...
- (转)解决ScrollView嵌套ListView或者GridView导致只显示一行的方法
即动态获取ListView和GridView的高度 一.对于ListView ListView listview= new ListView(this); setListViewHeightBased ...
- C语言-12-日期和时间处理标准库详细解析及示例
概述 标准库 提供了用于日期和时间处理的结构和函数 是C++语言日期和时间处理的基础 与时间相关的类型 clock_t,本质是:unsigned long typedef unsigned long ...
- This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has p
2014-09-16 15:47:51.590:WARN:oejs.ErrorPageErrorHandler:EXCEPTION org.apache.jasper.JasperException: ...
- 为什么Erlang比C慢那么多倍?
Erlang 一直以慢“著称”,本文就来看看 Erlang 慢在什么地方,为什么比实现同样功能的 C 语言程序慢那么多倍.Erlang 作为一种虚拟机解释的语言,慢是当然的.不过本文从细节上分析为什么 ...
- Effective Java 34 Emulate extensible enums with interfaces
Advantage Disadvantage Enum types Clarity Safety Ease of maintenance. None extensibility Typesafe en ...
- 使用MiniProfiler调试ASP.NET MVC网站性能
MiniProfiler 以前开发Webform的时候可以开启trace来跟踪页面事件,这对于诊断程序的性能是有很大的帮助的,起到事半功倍的作用,今天我就来谈用mvc开 发项目的调试和性能监控.EF框 ...