洛谷 P1886 滑动窗口 (数据与其他网站不同。。)
2024-08-29 23:07:57
题目描述
现在有一堆数字共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 ;
}
最新文章
- Linux 命令基础合集
- #9.5课堂JS总结#循环语句、函数
- method
- java--构造器初始化
- QWT6.0.1+win7下安装说明
- 支持BLOB操作的Jena框架扩展——JenaBLOB
- BIOS与UEFI、MBR和GPT介绍
- 隐藏 Status Bar
- 【HDU 2586 How far away?】LCA问题 Tarjan算法
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider
- ubuntu 10.04 下 samba 服务的配置
- Excel教程(12) - 数学和三角函数
- JDBC连接SQL Server 2005步骤详解
- Mybatis Dynamic Query 1.0.2版本
- Windows Azure Virtual Machine (34) Azure VM挂载WebDAV
- 微信小程序echarts层级太高
- 教你快速撸一个免费HTTPS证书
- mongodb+express+nodejs(登陆退出)
- Delphi 使用MD5 比对文件
- sprint boot 配置
热门文章
- 优化VMware提高虚拟机运行速度的技巧
- 关于spring cloud eureka整合ribbon实现客户端的负载均衡
- RestTemplate中headers中添加Host不生效
- Linux下 本地yum源搭建
- bzoj 3144 [Hnoi2013]切糕【最小割+dinic】
- UVA - 10859 Placing Lampposts 放置街灯
- 深入学习Ajax
- Lightoj 1054 - Efficient Pseudo Code
- 转-UIButton定义和设置圆角
- Codeforces Beta Round #96 (Div. 2) (A-E)