最近BZOJ炸了,而我的博客上又更新了一些基本知识,所以这里刷一些裸题,用以丰富知识性博客

  POJ2823   滑动的窗口

  这是一道经典的单调队题,我记得我刚学的时候就是用这道题作为单调队列的例题,算一道比较基本的题目

  先贴题目

Description

An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example: 
The array is [1 3 -1 -3 5 3 6 7], and k is 3.

Window position Minimum value Maximum value
[1  3  -1] -3  5  3  6  7  -1 3
 1 [3  -1  -3] 5  3  6  7  -3 3
 1  3 [-1  -3  5] 3  6  7  -3 5
 1  3  -1 [-3  5  3] 6  7  -3 5
 1  3  -1  -3 [5  3  6] 7  3 6
 1  3  -1  -3  5 [3  6  7] 3 7

Your task is to determine the maximum and minimum values in the sliding window at each position. 

Input

The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line. 

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values. 

Sample Input

8 3
1 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 3
3 3 5 5 6 7
  这道题的题目经过思考发现是关于决策的,而状态的转移又有固定的模式,所以是DP。
  那么DP方程是什么捏?
  这个经思考很好得出f[i]=max(f[i],a[k])和g[i]=min(g[i],a[k]),k都是从i-k+1到i;
  那么显然的,这个方法不TLE就见鬼了。虽然题目给了你12秒但是你也不能这样胡做
  所以我们考虑更优的解法;
  很容易看出来,我们原方程的每个i的决策与前一个或后一个决策都有k-1个决策重复,而对于每一个决策k的结果,都和i无关,所以我们可以优化这一过程。
  因为当我们更新完第i个位置的最优解的时候,下一个元素的最优解可以用只判断一个元素来更新。
  所以就可以用单调队列了啊(不会的面壁)。
然后愉快的贴出代码。
 #include<cstdio>
#include<cstring>
int quq[],ass,n,k,star,a[],time[];
int main()
{ scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)scanf("%d",a+i);
star=,ass=;
quq[star]=a[];
time[ass]=;
for(int i=;i<=k;i++)
{
while(a[i]<=quq[ass]&&ass>=star)--ass;
quq[++ass]=a[i];
time[ass]=i;
}
printf("%d ",quq[star]);
for(int i=k+;i<=n;i++)
{
if(time[star]<=i-k)star++;
while(a[i]<=quq[ass]&&ass>=star)--ass;
quq[++ass]=a[i];
time[ass]=i;
printf("%d ",quq[star]);
}
printf("\n");
star=,ass=;
quq[star]=a[];
time[ass]=;
for(int i=;i<=k;i++)
{
while(a[i]>=quq[ass]&&ass>=star)--ass;
quq[++ass]=a[i];
time[ass]=i;
}
printf("%d ",quq[star]);
for(int i=k+;i<=n;i++)
{
if(time[star]<=i-k)star++;
while(a[i]>=quq[ass]&&ass>=star)--ass;
quq[++ass]=a[i];
time[ass]=i;
printf("%d ",quq[star]);
}
return ;
}

 

刷题向》POJ2823 单调队列裸题(<不会做,请自裁>系列)的更多相关文章

  1. [Usaco2006 Mar]Mooo 奶牛的歌声(单调栈裸题)

    1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 961  Solved: 679[Submi ...

  2. luoguP1886 滑动窗口(单调队列模板题)

    题目链接:https://www.luogu.org/problem/P1886#submit 题意:给定n个数,求大小为k的滑动窗口中最小值和最大值. 思路:单调队列模板题. AC代码: #incl ...

  3. Sliding Window POJ - 2823 单调队列模板题

    Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...

  4. poj2823:单调队列入门题

    今天学习了一下单调队列这种数据结构,思想不是很难 参考资料:http://www.cnblogs.com/Jason-Damon/archive/2012/04/19/2457889.html 然后自 ...

  5. 2019年牛客多校第三场 F题Planting Trees(单调队列)

    题目链接 传送门 题意 给你一个\(n\times n\)的矩形,要你求出一个面积最大的矩形使得这个矩形内的最大值减最小值小于等于\(M\). 思路 单调队列滚动窗口. 比赛的时候我的想法是先枚举长度 ...

  6. 斜率优化第一题! HDU3507 | 单调队列优化DP

    放一手原题 题解: 第一次写(抄)斜率优化,心里还是有点小激动的.讲一下怎么实现的! 首先我们可以考虑一个朴素的dp:DP[i]表示前i个数字的最少花费,显然我们有一个转移方程 DP[i]=min{D ...

  7. hdu3415 单调队列模板题

    比较裸的单调队列 先求前缀和,枚举所有结束位置1~n+k即可 #include<iostream> #include<cstdio> #include<cstring&g ...

  8. caioj 1172 poj 2823 单调队列过渡题

    给定一个n个数的数列,从左至右输出每个长度为m的数列段内的最大数. 输入:第一行两个整数n和m( 1<= n <= 20 0000,m<=n).下来给出n个整数. 输出:一行一个整数 ...

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

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

随机推荐

  1. AS3里面的错误代码

    ActionScript 3 出现2048安全策略服务,一种原因是因为843端口下发策略文件有问题,另一种原因是Socket端口有问题,可以用telnet来测试. 其它AS3错误代码的意义可以在官网文 ...

  2. VC++6.0/MFC中如何限制Edit控件输入 例子,只能输入0和1

    1.Insert -> New Class -> 在Base Class中选择CEdit,在Name中输入CMyEdit. 2.在左边的ClassView中,右键击CMyEdit,选择Ad ...

  3. 【转载】获取MAC地址方法大全

    From:http://blog.csdn.net/han2814675/article/details/6223617 Windows平台下用C++代码取得机器的MAC地址并不是一件简单直接的事情. ...

  4. LeetCode Valid Triangle Number

    原题链接在这里:https://leetcode.com/problems/valid-triangle-number/description/ 题目: Given an array consists ...

  5. CentOS 6+Hadoop 2.6.0分布式集群安装

    1.角色分配 IP Role Hostname 192.168.18.37 Master/NameNode/JobTracker HDP1 192.168.18.35 Slave/DataNode/T ...

  6. throw、try 和 catch

    try 语句允许我们定义在执行时进行错误测试的代码块. catch 语句允许我们定义当 try 代码块发生错误时,所执行的代码块. JavaScript 语句 try 和 catch 是成对出现的. ...

  7. rpm 软件包离线安装

    1. 插件安装(可选) yum install yum-plugin-downloadonly 2. 只下载不安装(perl 演示) yum install --downloadonly --down ...

  8. PowerDesigner导出word表结构

    一.wordTemplate.rtp下载 首先下载wordTemplate.rtp,将该文件放在一下路径下 C:\Program Files (x86)\Sybase\PowerDesigner 16 ...

  9. PADS Logic 脚本的 Fields 一个对象记录

    PADS Logic 脚本的 Fields 一个对象记录 PADS Laogic 有一个非常棒的脚本功能,可以导出所以元件. 我目前是把脚本绑定到 Ctrl+S 上,在保存时自动导出 txt 文件,方 ...

  10. php与JAVA的RSA加密互通

    Java 版本RSA 进行加密解密 在网上查询了好几天,最终找到解决方案,网络上都是通过Cipher.getInstance("RSA"); 而改成Cipher.getInstan ...