【BZOJ2823】Sliding Window

经典单调队列

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 const int N=1000010;
 5 int maxq[N],minq[N],a[N],ans1[N],ans2[N],lmax,lmin,rmax,rmin;
 6 int main(){
 7     int n,k;
 8     lmax=lmin=rmax=rmin=0;
 9     scanf("%d %d",&n,&k);
10     for (int i=1;i<=n;i++)
11         scanf("%d",&a[i]);
12     for (int i=1;i<=n;i++){
13         while (lmax<=rmax&&maxq[lmax]<=i-k) lmax++;
14         while (lmin<=rmin&&minq[lmin]<=i-k) lmin++;
15         while (rmax>=lmax&&a[maxq[rmax]]<a[i]) rmax--;
16         maxq[++rmax]=i;
17         while (rmin>=lmin&&a[minq[rmin]]>a[i]) rmin--;
18         minq[++rmin]=i;
19         ans1[i]=a[minq[lmin]];
20         ans2[i]=a[maxq[lmax]];
21     }
22     for (int i=k;i<=n;i++)
23         printf("%d ",ans1[i]);
24     printf("\n");
25     for (int i=k;i<=n;i++)
26         printf("%d ",ans2[i]);
27     return 0;
28 }

STD

时间: 2024-08-15 06:03:36

【BZOJ2823】Sliding Window的相关文章

【Leetcode】Sliding Window Maximum

题目链接:https://leetcode.com/problems/sliding-window-maximum/ 题目: Given an array nums, 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 sli

【原创】Sliding Window Maximum 解法分析

这道题是lintcode上的一道题,当然leetcode上同样有. 本题需要寻找O(N)复杂度的算法. 解体思路比较有特点,所以容易想到参考 最小栈 的解题办法. 但是最小栈用栈维护最小值很直观,这道题是队列,用什么数据结构好呢?也许看完暴力解会有点启发. 但是思路还是一样的,最大的要在最前面(直接获取结果),小的值在后面保留下来(防止之后遍历到的时候丢失数据).并且某值出窗口的时候需要判断是否要修改排在最前面的值. 一.暴力解 当然直观看,暴力求解是 O(NK)的复杂度,大体的代码如下:(写的

【leetcode】Minimum Window Substring

问题: 给定两个字符串,S,T,返回S中包含T中所有字符的最短的字串,若不存在,则返回"".时间复杂度为O(n). 例如:S = "ADOBCODEBANC" T = "ABC" 返回BANC 生活场景: 把问题具体化现实化一点.有n层楼,每层楼里放有一个物品,现在老板给你一个物品清单,里面是要你集齐的物品,你可以乘坐电梯,但是电梯只停一次,停在哪一层,就从哪一层开始向楼上搜集物品,至于要在那层停电梯,由你自己选择. 这里我们当然选择即能集齐物品

【LeetCode 239】Sliding Window Maximum

Given an array nums, 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 right by one position. For example,Given nums

【POJ 2823 Sliding Window】 单调队列

题目大意:给n个数,一个长度为k(k<n)的闭区间从0滑动到n,求滑动中区间的最大值序列和最小值序列. 最大值和最小值是类似的,在此以最大值为例分析. 数据结构要求:能保存最多k个元素,快速取得最大值,更新时删去“过期”元素和“不再有希望”的元素,安放新元素. 单调队列的基本概念百度百科讲得比较清楚了:http://baike.baidu.com/view/3771451.htm 我的大致思路是: 1. 每个元素存储为结构体,包含它的秩和值.维护最大长度为k的单调队列,保证所有元素的秩都在区间内

【leetcode】Minimum Window Substring (hard) ★

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". Note:If there is no such window i

【Chromium】sandboxed window问题记录

问题发现 在业务逻辑中发现有时使用chrome.app.window.create这个API创建出来的窗口无法使用其他的API,不仅其他chrome.app.window的API说window is undefined而且还有奇怪的警告和报错 Creating sandboxed window, it doesn't have access to the chrome.app API. The chrome.app.window.create callback will be called, b

【MongoDB】在window系统下搭建MongoDB的分片系统(一)

这篇主要讲述分片集群的主要原理 坦白说,刚看到这个分片系统(Sharding)有点蒙,感觉有点太高大上了.看美国作家Kyle Banker<Mongodb in action>没有明白.又查询资料,首先对与分片的做个说明.从其他书本上看的,说分片这是一种将海量数据水平扩展的数据库集群系统,数据分表存储在sharding的各个节点上,使用者通过简单的配置就可以很方便地够将一个分布式MongoDB集群. 一.角色说明 要构建一个MongoDB分片集群,需要三个角色: shard server  即

【BZOJ2823】【AHOI2012】信号塔 最小圆覆盖 计算几何

链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/46605193"); } 题解之前: 首先最小圆覆盖虽然有三层 for 循环,但是它是期望 O(n) 的.什么?你问我为啥?那我只能呵呵了,50W的 O(n3) 高速跑过. 后交的是不求凸包直接跑的,先交的是求了凸包再