codevs4373 窗口==poj2823 Sliding Window
| Time Limit: 12000MS | Memory Limit: 65536K | |
| Total Submissions: 53676 | Accepted: 15399 | |
| Case Time Limit: 5000MS | ||
Description
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
Output
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
给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表:
| Window position | Min value | Max 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 |
你的任务是找出窗口在各位置时的max value, min value.
第1行n,k,第2行为长度为n的数组
2行
第1行每个位置的min value
第2行每个位置的max value
8 3
1 3 -1 -3 5 3 6 7
-1 -3 -3 -3 3 3
3 3 5 5 6 7
数据范围:20%: n<=500; 50%: n<=100000;100%: n<=1000000;
分类标签 Tags 点此展开



2017-03-27
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=1e6+;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,a[N],q[N],id[N];
int main(){
n=read();m=read();
for(int i=;i<=n;i++) a[i]=read();
int h=,t=;
for(int i=;i<m;i++){
while(h<t&&i-id[h]>=m) h++;
while(h<t&&q[t-]>a[i]) t--;
q[t]=a[i];id[t++]=i;
}
for(int i=m;i<=n;i++){
while(h<t&&i-id[h]>=m) h++;
while(h<t&&q[t-]>a[i]) t--;
q[t]=a[i];id[t++]=i;
if(h<t) printf("%d ",q[h]);
}
putchar('\n');
h=,t=;
for(int i=;i<m;i++){
while(h<t&&i-id[h]>=m) h++;
while(h<t&&q[t-]<a[i]) t--;
q[t]=a[i];id[t++]=i;
}
for(int i=m;i<=n;i++){
while(h<t&&i-id[h]>=m) h++;
while(h<t&&q[t-]<a[i]) t--;
q[t]=a[i];id[t++]=i;
if(h<t) printf("%d ",q[h]);
}
return ;
}
#include<cstdio>
using namespace std;
inline int read(){
register int x=;bool f=;
register char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return f?x:-x;
}
const int N=1e6+;
int n,k,a[N],num[N],q[N];
int main(){
n=read();k=read();
for(int i=;i<=n;i++) a[i]=read();
int l=,r=;
for(int i=;i<k;i++){
for(;l<=r&&i-num[l]>=k;l++);
for(;l<=r&&a[i]<q[r];r--);
q[++r]=a[i];num[r]=i;
}
for(int i=k;i<=n;i++){
for(;l<=r&&i-num[l]>=k;l++);
for(;l<=r&&a[i]<q[r];r--);
q[++r]=a[i];num[r]=i;
printf("%d ",q[l]);
}
putchar('\n');
l=,r=;
for(int i=;i<k;i++){
for(;l<=r&&i-num[l]>=k;l++);
for(;l<=r&&a[i]>q[r];r--);
q[++r]=a[i];num[r]=i;
}
for(int i=k;i<=n;i++){
for(;l<=r&&i-num[l]>=k;l++);
for(;l<=r&&a[i]>q[r];r--);
q[++r]=a[i];num[r]=i;
printf("%d ",q[l]);
}
return ;
}
codevs4373 窗口==poj2823 Sliding Window的更多相关文章
- POJ2823 Sliding Window (单调队列)
POJ2823 Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 38342 Accepte ...
- 滑动窗口(Sliding Window)技巧总结
什么是滑动窗口(Sliding Window) The Sliding Problem contains a sliding window which is a sub – list that run ...
- [Swift]LeetCode239. 滑动窗口最大值 | Sliding Window Maximum
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- 数据流滑动窗口平均值 · sliding window average from data stream
[抄题]: 给出一串整数流和窗口大小,计算滑动窗口中所有整数的平均值. MovingAverage m = new MovingAverage(3); m.next(1) = 1 // 返回 1.00 ...
- 【LeetCode】480. 滑动窗口中位数 Sliding Window Median(C++)
作者: 负雪明烛 id: fuxuemingzhu 公众号: 每日算法题 本文关键词:LeetCode,力扣,算法,算法题,滑动窗口,中位数,multiset,刷题群 目录 题目描述 题目大意 解题方 ...
- POJ2823 Sliding Window
Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 53086 Accepted: 15227 Case Time Limi ...
- POJ2823 Sliding Window(单调队列)
单调队列,我用deque维护.这道题不难写,我第二次写单调队列,1次AC. -------------------------------------------------------------- ...
- [POJ2823]Sliding Window 滑动窗口(单调队列)
题意 刚学单调队列的时候做过 现在重新做一次 一个很经典的题目 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗 ...
- poj2823 Sliding Window luogu1886 滑动窗口 单调队列
模板题 #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...
随机推荐
- Spring MVC BeanNameUrlHandlerMapping example
In Spring MVC, BeanNameUrlHandlerMapping is the default handler mapping mechanism, which maps URL re ...
- Spring Data JPA教程, 第八部分:Adding Functionality to a Repository (未翻译)
The previous part of my tutorial described how you can paginate query results with Spring Data JPA. ...
- C++的辅助工具介绍
1 文档类 (1) Doxygen 参考站点:http://www.doxygen.org Doxygen是一种适合C风格语言(如C++.C.IDL.Java甚至包括C#和PHP)的.开放源码的 ...
- 神经网络学习-问题(二)-scipy未正确安装报DLL找不到的问题
问题如下: E:\project\DL\python\keras>python keras_sample.pyUsing Theano backend.Traceback (most recen ...
- 一个非常标准的Java连接Oracle数据库的示例代码
最基本的Oracle数据库连接代码(只针对Oracle11g): 1.右键项目->构建路径->配置构建路径,选择第三项“库”,然后点击“添加外部Jar”,选择“D:\Oracle\app\ ...
- Oracle数据库如何授权收费(Database Licensing)
Oracle软件本身是免费的,所以任何人都可以从Oracle官方网站下载并安装Oracle的数据库软件,收费的是License,即软件授权,如果数据库用于商业用途,就需要购买相应Oracle产品的Li ...
- UVa572 Oil Deposits DFS求连通块
技巧:遍历8个方向 ; dr <= ; dr++) ; dc <= ; dc++) || dc != ) dfs(r+dr, c+dc, id); 我的解法: #include< ...
- 文件映射spring 使用classpath方式加载hibernate映射文件
在改章节中,我们主要介绍文件映射的内容,自我感觉有个不错的建议和大家分享下 <!-- 批量指定到classpath下面 --> <property name="mappin ...
- Codeforces Gym 100418J Lucky tickets 数位DP
Lucky ticketsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...
- 数据库插入某些数据会变成?,或则select无法读出数据库中的某些数据
如果你想在数据库中插入“uɷ”,这个字符,直接插入 insert into table value (‘uɷ’),是不行的,这样插入的后果是打开数据后会显示为u?.当你面对这个问题的时候是不是第一个想 ...