POJ 2823 Sliding Window (滑动窗口的最值问题 )
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 41264 | Accepted: 12229 | |
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
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
我按照自己的思路进行了部分优化模拟,仍然超时。
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <deque> using namespace std; struct node
{
int mi;
int ma;
}q[1000004]; int a[1000004]; int main()
{
int n, k;
int i, j; scanf("%d %d", &n, &k);
for(int i=0; i<n; i++)
scanf("%d", &a[i] );
int dd=a[0], ff=a[0];
for(i=1; i<k; i++)
{
if(a[i]>dd) dd=a[i]; //max
if(a[i]<ff) ff=a[i]; //min
}
int e=0;
q[e].ma=dd; q[e++].mi=ff;
int pos; for(j=k; j<n; j++ )// 遍历这些序列
{
if(a[j]>dd) dd=a[j];
if(a[j]<ff) ff=a[j]; //判断是不是要更新
pos=j-k;
if(a[pos]>dd && a[pos]<ff )
continue;
else
{
int p, w;
if(a[pos]==dd) //起点是==最大值 更新最大值
{
p=a[pos+1];
for(i=pos+2; i<=j; i++)
{
p=max(p, a[i]);
}
dd=p;
}
else if(a[pos]==ff) //起点是==最小值 更新最小值
{
w=a[pos+1];
for(i=pos+2; i<=j; i++)
{
w=min(w, a[i]);
}
ff=w;
}
q[e].ma=dd; q[e++].mi=ff;
}
}
for(i=0; i<e; i++)
{
if(i==e-1)
printf("%d\n", q[i].mi);
else
printf("%d ", q[i].mi);
}
for(i=0; i<e; i++)
{
if(i==e-1)
printf("%d\n", q[i].ma );
else
printf("%d ", q[i].ma );
}
return 0;
}
POJ 2823 Sliding Window (滑动窗口的最值问题 )的更多相关文章
- 洛谷P1886 滑动窗口(POJ.2823 Sliding Window)(区间最值)
To 洛谷.1886 滑动窗口 To POJ.2823 Sliding Window 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每 ...
- POJ 2823 Sliding Window + 单调队列
一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1) 从队首删除 (2) 从队尾删除 (3) 从队尾插入 (4) ...
- POJ 2823 Sliding Window 题解
POJ 2823 Sliding Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...
- POJ 2823 Sliding Window & Luogu P1886 滑动窗口
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 66613 Accepted: 18914 ...
- POJ - 2823 Sliding Window (滑动窗口入门)
An array of size n ≤ 10 6 is given to you. There is a sliding window of size kwhich is moving from t ...
- 【POJ 2823】【Luogu P1886】Sliding Window 滑动窗口
POJ 2823 Luogu P1886 [解题思路] 这是一个单调队列算法的经典题目,几乎学习单调队列的人都接触过这题. 利用单调队列算法求出每一个固定区间内的最(大/小)值. 以下以最大值为例: ...
- POJ 2823 Sliding Window(单调队列入门题)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 67218 Accepted: 190 ...
- poj 2823 Sliding Window (单调队列入门)
/***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...
- POJ 2823 Sliding Window ST RMQ
Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...
随机推荐
- 「NOI2014」动物园
link : https://loj.ac/problem/2246 水水KMP #include<bits/stdc++.h> #define ll long long #define ...
- 邁向IT專家成功之路的三十則鐵律 鐵律六:求全求盈之道-佈施
如果您只是在IT方面的專業技術與經驗相當高超,而不懂得在日常生活之中當一位俠義肝膽之人,來隨時隨地伸手幫助身旁需要幫助的人,那麼您只能算是一位有勇有謀但卻無智慧的匹夫罷了.既是匹夫那麼即便成功也會是短 ...
- 【java】spring项目中 对entity进行本类间的克隆
方法1: [使用spring自带BeanUtils实现克隆] [要求:需要被克隆的类实现Cloneable接口并且重写clone()方法] >例子: >>实体: package co ...
- cocos2d-x调用android内嵌浏览器打开网页
cocos2d-x调用android内嵌浏览器打开网页,能够从入口传入网址,C++调用android 的api就可以实现. 方法也非常easy 1. 改动"cocos2dx\platform ...
- mysql报错锦集
MySQL 启动报错 - ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/ ...
- <LeetCode OJ> 337. House Robber III
Total Accepted: 1341 Total Submissions: 3744 Difficulty: Medium The thief has found himself a new pl ...
- cocos2dx3.0 对象池
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdzE4NzY3MTA0MTgz/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- 最新研发的基于Java的高速开发平台
可自我扩展的智能开发平台 在开发平台设计过程中,联科研发部一開始就希望能研发一套智能开发机制能自己开发自己的平台-即一个能自我修复和自我扩展的开发平台.这个开发平台不但能开发其它应用还能不 ...
- MGTemplateEngine 模版发动机简单使用
https://github.com/nxtbgthng/MGTemplateEngine MGTemplateEngine 模版引擎 MGTemplateEngine比較象 PHP 中的 Smart ...
- h5页面测试
转自:http://www.blogjava.net/qileilove/archive/2014/07/24/416154.html?utm_source=tuicool&utm_mediu ...