最近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. Leetcode 1005. Maximize Sum Of Array After K Negations

    class Solution(object): def largestSumAfterKNegations(self, A, K): """ :type A: List[ ...

  2. 绕过SQL限制的方法

    突然想我们是否可以用什么方法绕过SQL注入的限制呢?到网上考察了一下,提到的方法大多都是针对AND与“’”号和“=”号过滤的突破,虽然有点进步的地方,但还是有一些关键字没有绕过,由于我不常入侵网站所以 ...

  3. 正则化项L1和L2的区别

    https://blog.csdn.net/jinping_shi/article/details/52433975 https://blog.csdn.net/zouxy09/article/det ...

  4. linux下json库的编译及例程

    .下载JsonCpp http://sourceforge.net/projects/jsoncpp/files/ .下载scons http://sourceforge.net/projects/s ...

  5. BZOJ - 3224 Tyvj 1728 普通平衡树 (treap/树状数组)

    题目链接 treap及树状数组模板题. treap版: #include<bits/stdc++.h> using namespace std; typedef long long ll; ...

  6. BZOJ - 5427:最长上升子序列 (二分&思维)

    现在给你一个长度为n的整数序列,其中有一些数已经模糊不清了,现在请你任意确定这些整数的值, 使得最长上升子序列最长.(为何最长呢?因为hxy向来对自己的rp很有信心)   Input 第一行一个正整数 ...

  7. 几个开源faas 框架

    funktion open source event based lambda programming for kubernetes 官方地址: funktion.fabric8.io serverl ...

  8. C# 实现程序只启动一次(实现程序自重启)

    程序运行过程中,不能有多个实例运行,并且需要程序自己可以重启(重新运行),所以代码如果下代码: static void Main() { bool createNew; using (System.T ...

  9. 记一次RESTful调试过程

    1. 为什么前台怎么调用后台,都是跳到页面不存在. 因为已经改为RESTful,保存按钮的type还是submit: 2. 改成buttong之后,设置onclick="update()“, ...

  10. MySQL删除数据库

    drop命令用于删除数据库. drop命令格式:drop database <数据库名>; 例如,删除名为 xhkdb的数据库: mysql> drop database xhkdb ...