POJ 2823
2024-09-25 14:29:18
Sliding Window
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 35941 | Accepted: 10636 | |
Case Time Limit: 5000MS |
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.
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
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
Source
POJ Monthly--2006.04.28, Ikki
RMQ
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int MAX_N = 1e6 + ;
int N,K;
int mi[MAX_N],a[MAX_N],ma[MAX_N];
int k; void RMQ() {
for(int j = ; j <= k; ++j) {
for(int i = ; i + ( << j) - <= N; ++i) {
mi[i] = min(mi[i],mi[i + ( << (j - ))]);
ma[i] = max(ma[i],ma[i + ( << (j - ))]);
}
}
} int main()
{
//freopen("sw.in","r",stdin);
while(~scanf("%d%d",&N,&K)) {
for(int i = ; i <= N; ++i) {
scanf("%d",&a[i]);
ma[i] = mi[i] = a[i];
} k = ;
while(( << (k + )) < K) ++k;
RMQ();
for(int i = ; i <= N - K + ; ++i) {
printf("%d%c",min(mi[i],mi[i + K - ( << k)]),i == N - K + ? '\n' : ' ');
} for(int i = ; i <= N - K + ; ++i) {
printf("%d%c",max(ma[i],ma[i + K - ( << k)]),i == N - K + ? '\n' : ' ');
} } return ;
}
单调队列
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int MAX_N = 1e6 + ;
int N,K;
int a[MAX_N];
int mi[ * MAX_N],ma[ * MAX_N];
int ans[MAX_N]; void solve() {
int s = ,e = ;
for(int i = ; i <= N; ++i) {
while(s < e && a[i] < a[ mi[e - ] ]) --e;
mi[e++] = i;
if(i - K + >= ) {
ans[i - K + ] = a[ mi[s] ];
}
if(mi[s] == i - K + ) ++s;
} for(int i = ; i + K - <= N; ++i) {
printf("%d%c",ans[i],i == N - K + ? '\n' : ' ');
} s = ,e = ;
for(int i = ; i <= N; ++i) {
while(s < e && a[i] > a[ ma[e - ] ]) --e;
ma[e++] = i;
if(i - K + >= ) {
ans[i - K + ] = a [ ma[s] ];
}
if(ma[s] == i - K + ) ++s;
} for(int i = ; i + K - <= N; ++i) {
printf("%d%c",ans[i],i == N - K + ? '\n' : ' ');
} } int main() {
while(~scanf("%d%d",&N,&K)) {
for(int i = ; i <= N; ++i) {
scanf("%d",&a[i]);
} solve();
} return ; }
最新文章
- Ajax深入解析
- C# 扩展方法集
- vim 标签命令
- [linux]ubuntu 下安装RMySQL包
- sencha touch之模型(model)
- java---数据格式的验证
- KMP--路过
- ios 编译openssl支持arm64(转)
- mysql 重要维护工具 图解
- 求解决!!!SystemVerilog于ModelSim在编译和执行
- 对象javascript
- 对spring,struts,hibernate及MVC的理解
- 易语言 【寻找文本】命令的bug
- Divide Candies CodeForces - 1056B (数学)
- 【HDFS API编程】从本地拷贝文件,从本地拷贝大文件,拷贝HDFS文件到本地
- 更新 app 操作过期提示
- Cassandra的commitLog、memtable、 SStable
- Laravel中使用自己的类库三种方式
- Laravel 5 中使用 JWT(Json Web Token) 实现基于API的用户认证
- Atcoder Educational DP Contest
热门文章
- 【微网站开发】之微信内置浏览器API使用
- javascript 关于Date 时间类型 处理方法
- hdu 1702 ACboy needs your help again!
- Android内存管理机制
- Swift global function(count indexOfObject contains...)
- 条款24:若所有的函数参数可能都需要发生类型转换才能使用,请采用non-member函数
- [转]反向代理过程与Nginx特点详解
- typedef和自定义结构体类型
- 微软职位内部推荐-Senior SDE
- CentOS安装Git实现多人同步开发