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 ...
随机推荐
- 用LCT解一类动态图的问题
很显然,学过了LCT,大家一定都会用LCT来维护动态树结构了 那么,遇到图问题的时候,是不是也能用lct来解决呢? 解决图问题的时候,我们必须要仍然维护一棵树的形态,否则,lct是做不动的 那么下面来 ...
- 在Dev GridControl中添加颜色可变的ProgressBar z
在使用DevExpress,GridControl自带的ProgressBarControl的时候 由于无法通过BackColor/ForeColor来改变进度条的颜色所以很多特效是实现不了的.如下面 ...
- 投影纹理映射(Projective Texture Mapping) 【转】
摘抄“GPU Programming And Cg Language Primer 1rd Edition” 中文名“GPU编程与CG语言之阳春白雪下里巴人” 投影纹理映射( Projective ...
- 漫谈深度学习时代点击率预估技术进展 &&深度学习在推荐系统上的发展
转载:https://www.infoq.cn/article/XA055tpFrprUy*0UBdCb https://www.zhihu.com/question/20830906/answer/ ...
- 南阳 oj 表达式求值 题目35 数据结构 NYO题目链接
建议不会的看别人的代码自己在之上模拟一遍,仅仅要耐心模拟就会做出来 题目链接:http://acm.nyist.net/JudgeOnline/problem.php? pid=35 #incl ...
- Python+Selenium框架-unittest执行脚本方法之addTest
本文开始介绍如何通过unittest来管理和执行测试用例,这一篇介绍unittest下addTest()方法来加载测试用例到测试套件中去.为了演示效果,我在前面文章的脚本基础上,新建了一个测试脚本,这 ...
- Linux下Tun/Tap设备通信原理
Tun/Tap都是虚拟网卡,没有直接映射到物理网卡,是一种纯软件的实现.Tun是三层虚拟设备,能够处理三层即IP包,Tap是二层设备,能处理链路层网络包如以太网包.使用虚拟网络设备,可以实现隧道,如O ...
- 关于CSS和CSS3的布局小知识(干货)
最近在网站偶然看到的这个网站,进去看了下讲的CSS布局,感觉还不错,讲易懂且实用推荐给大家. http://zh.learnlayout.com/
- JSP学习笔记(一)
JSP是基于JAVA语言的,区分大小写,HTML不区分大小写 如何建立Web服务目录? 1.在Webapps下面建立Web服务目录MYJSP 在Webapps下面新建文件夹MYJSP,将写好的jsp文 ...
- 目标检测之hough forest---霍夫森林(Hough Forest)目标检测算法
Hough Forest目标检测一种比较时兴的目标检测算法,Juergen Gall在2009的CVPR上提出. Hough Forest听上去像hough变换+Random Forest的结合体, ...