L - Sliding Window

Time Limit:6000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu

Appoint description: 
System Crawler  (2016-04-24)

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

| 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

Hint

The data used in this problem is unofficial data prepared by love8909. So any mistake here does not imply mistake in the offcial judge data.

给定一个大小已知的数组以及一个大小已知的滑动窗口,窗口每个时刻向后移动一位,求出每个时刻窗口中数字的最大值和最小值。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const double pi=acos(-1);
const int maxn=1000000;
int a[maxn+10],deq[maxn+10],ans[maxn+10],n,k; void minq()
{
int s=1,t=0;
for(int i=1;i<=n;i++)
{
while(s<=t&&a[deq[t]]>a[i]) t--;//这个地方加不加等号貌似都可以
deq[++t]=i;
if(i>=k)
{
ans[i-k+1]=a[deq[s]];
if(deq[s]==i-k+1)
s++;
}
}
for(int i=1;i<n-k+1;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[n-k+1]);
} void maxq()
{
int s=1,t=0;
for(int i=1;i<=n;i++)
{
while(s<=t&&a[deq[t]]<a[i]) t--;
deq[++t]=i;
if(i>=k)
{
ans[i-k+1]=a[deq[s]];
if(deq[s]==i-k+1)
s++;
}
}
for(int i=1;i<n-k+1;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[n-k+1]);
} int main()
{
while(~scanf("%d %d",&n,&k))
{
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
minq();
maxq();
}
return 0;
}

  

TTTTTTTTTTTTTT CDOJ Sliding Window 线段树(nlogn)或双端队列(n) 模板的更多相关文章

  1. Gym 101911E "Painting the Fence"(线段树区间更新+双端队列)

    传送门 题意: 庭院中有 n 个围栏,每个围栏上都被涂上了不同的颜色(数字表示): 有 m 条指令,每条指令给出一个整数 x ,你要做的就是将区间[ x第一次出现的位置 , x最后出现的位置 ]中的围 ...

  2. POJ 2823 Sliding Window 线段树

    http://poj.org/problem?id=2823 出太阳啦~^ ^被子拿去晒了~晚上还要数学建模,刚才躺在床上休息一下就睡着了,哼,还好我强大,没有感冒. 话说今年校运会怎么没下雨!!!说 ...

  3. POJ 2823 Sliding Window 线段树区间求和问题

    题目链接 线段树区间求和问题,维护一个最大值一个最小值即可,线段树要用C++交才能过. 注意这道题不是求三个数的最大值最小值,是求k个的. 本题数据量较大,不能用N建树,用n建树. 还有一种做法是单调 ...

  4. PKU 2823 Sliding Window(线段树||RMQ||单调队列)

    题目大意:原题链接(定长区间求最值) 给定长为n的数组,求出每k个数之间的最小/大值. 解法一:线段树 segtree节点存储区间的最小/大值 Query_min(int p,int l,int r, ...

  5. 【POJ-2482】Stars in your window 线段树 + 扫描线

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11706   Accepted:  ...

  6. POJ 2482 Stars in Your Window 线段树扫描线

    Stars in Your Window   Description Fleeting time does not blur my memory of you. Can it really be 4 ...

  7. 【t019】window(线段树做法)

    Time Limit: 2 second Memory Limit: 256 MB [问题描述] 给你一个长度为N 的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右 ...

  8. POJ 2482 Stars in Your Window 线段树

    如果按一般的思路来想,去求窗户能框住的星星,就很难想出来. 如果换一个思路,找出每颗星星能被哪些窗户框住,这题就变得非常简单了. 不妨以每个窗户的中心代表每个窗户,那么每颗星星所对应的窗户的范围即以其 ...

  9. POJ 2482 Stars in Your Window (线段树区间合并+扫描线)

    这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用)  题意就是在平面上给你一些星 ...

随机推荐

  1. 关于Eclipse及JDK安装过程中的一些问题

    一,环境变量的配置 1.配置CLASSPATH系统变量 CLASSPATH系统变量为类查找路径 ①.在使用javac进行编译时遇到import时候就会通过这个变量里面配置的路径去查找.如果配置的是目录 ...

  2. 优秀编程学习网站&博文记录

    记录优秀讲解知识点博客内容,侵删! 编程者学习网站 LearnKu终身编程者的知识社区 自动化测试内容 Python 接口自动化测试 应用开源接口网站:https://httpbin.org/#/St ...

  3. 小记---------maxwell启动闪退问题

    日志报错信息如下:大致是说因为maxwell在对接mysql时伪装成一个从库slave,但是uuid重复.猜想是其他部门同事也在同时使用maxwell,都使用的是maxwell默认的uuid ,从而导 ...

  4. Axios 的基本使用

    Axios 是一个基于 promise 的HTTP 库, 可以用在浏览器和 node.js 中. 1. 从浏览器创建 XMLHttpRequests 2. 从node.js 创建 http 请求 3. ...

  5. 【挣扎失败】2019CSP-S 游记

    妈耶…… 今年是作为高中生参赛的第一年……然而…… 心痛 洛谷评分,一橙一蓝两紫两黑 我个菜鸡瑟瑟发抖 在考完后三天的信息课码的,刚写没几个字就要下课了 抽空把这个写完

  6. LOJ167 康托展开 题解

    题面 康托展开: 康托展开是一个全排列到一个自然数的双射,常用于构建哈希表时的空间压缩. 康托展开的实质是计算当前排列在所有由小到大全排列中的名次,因此是可逆的. X = A[0] * (n-1)! ...

  7. ctrNet库介绍

    一个神秘网友写的代码库,膜拜,附上下载链接:https://github.com/guoday/ctrNet-tool 似乎是专门为点击率预估写的库??? 收藏,日后慢慢研究

  8. 用C#控制台编写 推箱子之类的 坐标移动----之二维坐标

     //首先用枚举 列出方向  上,下,左,右(枚举的最后一位数后不用符号  否则会报错)        public enum dro    {       up = 1,        down = ...

  9. [转载]Linux运行模式及紧急、救援模式

    运行模式 在Linux中,存在一个叫init(initialize)的进程,其进程号是1,该进程存在一个对应的配置文件inittab,叫做系统的运行级别配置文件,位置在/etc/inittab.(但是 ...

  10. 28、周末看电影(每周五自动从top250中随机选取三部电影,并将下载链接发到邮箱里)

      练习介绍   在第3关的一个课后练习里,我们爬取了指定电影的下载链接:在第6关的一个课后练习里,我们存储了豆瓣TOP250的榜单信息.   现在,我们完善这个程序,每周五在存储的榜单中,随机抽三部 ...