最近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. ffmpeg推送RTSP直播流到EasyDarwin报错问题的修复

    在之前的博客<ffmpeg推送,EasyDarwin转发,vlc播放 实现整个RTSP直播>中,我们介绍了如何采用ffmpeg进行RTSP推送,实现EasyDarwin直播分发的功能,近期 ...

  2. make: *** No rule to make target `out/target/common/obj/APPS/framework-res_intermediates/src/R.stamp'

    /********************************************************************************** * make: *** No r ...

  3. 【数据库】MongoDB学习

    http://www.w3cschool.cc/mongodb/mongodb-tutorial.html http://api.mongodb.org/python/2.7rc0/examples/ ...

  4. STL源码解析之vector自实现

    最近仔细阅读了下侯捷老师的<STL源码剖析>的前三章内容:空间配置器.迭代器.traits技术,为了加强理解和掌握,特参照源码自实现Vector,以下对相关重点知识进行说明. 1. vec ...

  5. JDBC 3 通过PreparedStatement 对数据库进行增删改查

    下面程序沿用上面的封装. 1 插入数据 public boolean ChaRu3(User user){ boolean flag=true; Connection conn=null; Prepa ...

  6. C# 操作自定义config文件

    示例文件:DB.config 1.读取 //先实例化一个ExeConfigurationFileMap对象,把物理地址赋值到它的 ExeConfigFilename 属性中: ExeConfigura ...

  7. c#模拟键盘输入

    System.Windows.Forms.SendKeys.SendWait("j");

  8. 使用appassembler-maven-plugin插件生成启动脚本

    appassembler-maven-plugin可以自动生成跨平台的启动脚本,省去了手工写脚本的麻烦,而且还可以生成jsw的后台运行程序. 首先pom引入相关依赖 <build> < ...

  9. windows下通过Git Bash使用Git常用命令

    Git跟SVN最大不同的地方就是分布式.SVN的集中式与Git的分布式决定各自的业务场景.既然是分布式的,那么大部分操作就是本地操作.一般Git操作都是通过IDE,比如Eclipse,如果装了Git ...

  10. 有关implicit Intent的使用

    Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("geo:20.000,50.000&quo ...