最近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. 前端之HTML补充

    一.列表 (1).无序列表<ul> <body> <ul type="disc"> <li>属性一</li> <l ...

  2. Markdown list状态下插入代码

    /***************************************************************************** * Markdown list状态下插入代 ...

  3. (十八)js控制台方法

    console.log 以日志的形式打印 console.warn 输出警示信息 console.info 输出提示信息 console.error 输出错误信息 console.debug 输出调试 ...

  4. cocos2d-x3.16 default模式项目 Android Studio C++文件编译失败问题

    gradle sync正常,但是在编译的时候几乎自己写的Classes里全部c++文件的最后一行都在报错,原来是3.16 模板cpp-template-default内的Android.mk文件内这一 ...

  5. 使用js构造"ddMMMyy"格式的日期供postman使用(最low的方式)

    var date = new Date(); date.setDate(date.getDate() + 10); var year = date.getFullYear().toString().s ...

  6. java多线程:synchronized和lock比较浅析

    转载:http://www.toutiao.com/a6392135944652587266/?tt_from=weixin&utm_campaign=client_share&app ...

  7. Hadoop体系结构之 Mapreduce

    MR框架是由一个单独运行在主节点上的JobTracker和运行在每个集群从节点上的TaskTracker共同组成.主节点负责调度构成一个作业的所有任务,这些任务分布在不同的不同的从节点上.主节点监视它 ...

  8. laravel查看sql语句

    我自己是用第一种方法来调试的,第三种不行 不知道为啥 laravel查看sql语句 方法一: 我们有时候想测试一段代码生产的 SQL 语句,比如: 我们想看 App\User::all(); 产生的 ...

  9. 【转】jmeter压力测试

    jmeter压力测试 Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试但后来扩展到其他测试领域, 是压力测试的首选软件 ...

  10. Unity Shader入门教程(三)自制光照模型

    光照模型的概念目前还不明晰,因为笔者也是一个初学者,所以请小心对待笔者介绍的内容.笔者认为光照模型是规定光照算法的模型,比如说前面提到的Lambert光照模型,规定了材质表面的光线的表达式为 环境光+ ...