题目描述

现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口。现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值。

例如:

The array is [1 3 -1 -3 5 3 6 7], and k = 3.

输入输出格式

输入格式:

输入一共有两行,第一行为n,k。

第二行为n个数(<INT_MAX).

输出格式:

输出共两行,第一行为每次窗口滑动的最小值

第二行为每次窗口滑动的最大值

输入输出样例

输入样例#1:

8 3
1 3 -1 -3 5 3 6 7
输出样例#1:

-1 -3 -3 -3 3 3
3 3 5 5 6 7

说明

50%的数据,n<=10^5

100%的数据,n<=10^6

用了无数次线段树无数次80分后

我只想说

感(te)觉(ma)真(zhong)不(yu)错(guo)啊(le)

单调队列

屠龙宝刀点击就送

#include <ctype.h>
#include <cstdio>
#define gt ch=getchar()
#define N 1000005
void read(int &x)
{
x=;bool f=;
char ch;gt;
while(!isdigit(ch))
{
if(ch=='-') f=;
gt;
}
while(isdigit(ch))
{
x=x*+ch-'';
gt;
}
x=f?(~x)+:x;
}
int ans_min[N],ans_max[N],Num[N],n,k,a[N+],Que[N],l=,r=;
int main()
{
read(n);read(k);
for(int i=;i<=n;i++) read(a[i]);
int i=;
for(i=;i<k;i++)
{
while(l<=r&&Que[r]>=a[i]) r--;
Que[++r]=a[i];
Num[r]=i;
}
for(;i<=n;i++)
{
while(l<=r&&Que[r]>=a[i]) r--;
Que[++r]=a[i];
Num[r]=i;
while(Num[l]<=i-k) l++;
ans_min[i-k+]=Que[l];
}
for(int i=;i<=n-k+;i++) printf("%d ",ans_min[i]);
printf("\n");
l=;r=;i=;
for(i=;i<k;i++)
{
while(l<=r&&a[i]>=Que[r]) r--;
Que[++r]=a[i];
Num[r]=i;
}
for(;i<=n;i++)
{
while(l<=r&&a[i]>=Que[r]) r--;
Que[++r]=a[i];
Num[r]=i;
while(Num[l]<=i-k) l++;
ans_max[i-k+]=Que[l];
}
for(i=;i<=n-k+;i++) printf("%d ",ans_max[i]);
return ;
}

最新文章

  1. Linux 命令基础合集
  2. #9.5课堂JS总结#循环语句、函数
  3. method
  4. java--构造器初始化
  5. QWT6.0.1+win7下安装说明
  6. 支持BLOB操作的Jena框架扩展——JenaBLOB
  7. BIOS与UEFI、MBR和GPT介绍
  8. 隐藏 Status Bar
  9. 【HDU 2586 How far away?】LCA问题 Tarjan算法
  10. Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider
  11. ubuntu 10.04 下 samba 服务的配置
  12. Excel教程(12) - 数学和三角函数
  13. JDBC连接SQL Server 2005步骤详解
  14. Mybatis Dynamic Query 1.0.2版本
  15. Windows Azure Virtual Machine (34) Azure VM挂载WebDAV
  16. 微信小程序echarts层级太高
  17. 教你快速撸一个免费HTTPS证书
  18. mongodb+express+nodejs(登陆退出)
  19. Delphi 使用MD5 比对文件
  20. sprint boot 配置

热门文章

  1. 优化VMware提高虚拟机运行速度的技巧
  2. 关于spring cloud eureka整合ribbon实现客户端的负载均衡
  3. RestTemplate中headers中添加Host不生效
  4. Linux下 本地yum源搭建
  5. bzoj 3144 [Hnoi2013]切糕【最小割+dinic】
  6. UVA - 10859 Placing Lampposts 放置街灯
  7. 深入学习Ajax
  8. Lightoj 1054 - Efficient Pseudo Code
  9. 转-UIButton定义和设置圆角
  10. Codeforces Beta Round #96 (Div. 2) (A-E)