UESTC_Sliding Window 2015 UESTC Training for Data Structures<Problem K>

K - Sliding Window

Time Limit: 18000/6000MS (Java/Others)     Memory Limit: 131072/131072KB (Java/Others)

Submit Status

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. Window position Minimum value Maximum value

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 and output

Sample Input Sample Output
8 3
1 3 -1 -3 5 3 6 7
-1 -3 -3 -3 3 3
3 3 5 5 6 7

Hint

The data used in this problem is unofficial data prepared by love8909. So any mistake here does not imply mistake in the offcial judge data.

解题报告

滑动窗口问题,我们可以在O(1)的时间内得到某个点的答案,就是维护一个单调队列,首先考虑最大值问题,我们考虑 i < j,且a[i] < a[j],显然可以得到a[i]是根本无用的(因为从左往右滑,a[j]未出之前a[i]根本不可能最优),因此我们只需维护一个单调递减的队列的即可,即可在O(1)的时间内得到某个点最优值.

插入时也要维护单调性,不再累述.

最小值同理.

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>

using namespace std;
const int maxn = 1e6 + 50;
int n,k,q[maxn],h[maxn];

int main(int argc,char *argv[])
{
  scanf("%d%d",&n,&k);
  int front = 0 , rear = 0 ;
  for(int i = 0 ; i < n ; ++ i)
   scanf("%d",&h[i]);
  // Judge
  if (k >= n)
   k = n;
  // Init
  q[rear++] = 0;
  for(int i = 1 ; i < k ; ++ i)
   {
          while(h[i] <= h[q[rear-1]] && front < rear)
         rear--;
       q[rear++] = i;
   }
  printf("%d",h[q[front]]);
  for(int i = k ; i < n ; ++ i)
   {
          while(front < rear && i - q[front] >= k)
           front++;
          while(h[i] <= h[q[rear-1]] && front < rear)
         rear--;
       q[rear++] = i;
       printf(" %d",h[q[front]]);
   }
  printf("\n");
  // ReInit
  front = 0 , rear = 0 , q[rear++] = 0;
  for(int i = 1 ; i < k ; ++ i)
   {
          while(h[i] >= h[q[rear-1]] && front < rear)
         rear--;
       q[rear++] = i;
   }
  printf("%d",h[q[front]]);
  for(int i = k ; i < n ; ++ i)
   {
          while(front < rear && i - q[front] >= k)
           front++;
          while(h[i] >= h[q[rear-1]] && front < rear)
         rear--;
       q[rear++] = i;
       printf(" %d",h[q[front]]);
   }
  printf("\n");
  return 0;
}
时间: 2024-08-04 16:43:44

UESTC_Sliding Window 2015 UESTC Training for Data Structures<Problem K>的相关文章

UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures&lt;Problem C&gt;

C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 朝为田舍郎,暮登天子堂.秋实大哥从小就怀抱有远大的理想,所以他开了一家快餐店. 秋实大哥根据菜的口感,给每一道菜一个唯一的CID,同时对于前来的客人,根据他们的口味喜好,秋实大哥会给每一个客人一个PID. 对于一个标号为PID的客人,他对标号为CID的菜的喜爱程度为PID∧CI

UESTC_Islands 2015 UESTC Training for Data Structures&lt;Problem J&gt;

J - Islands Time Limit: 30000/10000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status Deep in the Carribean, there is an island even stranger than the Monkey Island, dwelled by Horatio Torquemada Marley. Not only it has a re

UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures&lt;Problem D&gt;

D - 秋实大哥与战争 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 男儿何不带吴钩,收取关山五十州. 征战天下是秋实大哥一生的梦想,所以今天他又在练习一个对战游戏. 秋实大哥命令所有士兵从左到右排成了一行来抵挡敌人的攻击. 敌方每一次会攻击一个士兵,这个士兵就会阵亡,整个阵列就会从这个位置断开:同时有的时候已阵亡的士兵会受人赢气息感染而复活. 秋

UESTC_Rain in ACStar 2015 UESTC Training for Data Structures&lt;Problem L&gt;

L - Rain in ACStar Time Limit: 9000/3000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status Maybe you have heard of Super Cow AC who is the great general of ACM Empire. However, do you know where he is from? This is one of th

UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures&lt;Problem H&gt;

H - 秋实大哥打游戏 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status ”也许人生就是游戏,你却执意耕耘着春秋.” —— 秋实大哥叹道. 秋实大哥是一个喜欢玩游戏的人,相较于其他种类的游戏,秋实大哥更喜欢自由开放的沙盒游戏,尤其是minecraft. 现在,秋实大哥发现了N个独立的小岛(编号1,2,3.....N),于是他要把这些小岛连起来. 每一次

UESTC_秋实大哥去打工 2015 UESTC Training for Data Structures&lt;Problem G&gt;

G - 秋实大哥去打工 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 天行健,君子以自强不息.地势坤,君子以厚德载物. 天天过节的秋实大哥又要过节了,于是他要给心爱的妹子买礼物.但由于最近秋实大哥手头拮据,身为一个男人,他决定去打工! 秋实大哥来到一家广告公司.现在有n块矩形墙从左至右紧密排列,每一块高为Hi,宽为Wi. 公司要求秋实大哥找出一块最

UESTC_秋实大哥与家 2015 UESTC Training for Data Structures&lt;Problem E&gt;

E - 秋实大哥与家 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 秋实大哥是一个顾家的男人,他认为人生就是旅途,不管我们漂到哪,最终还是会回到温暖的地方——家. 所以他买了很多家具. 秋实大哥的家可以看成一个W×H的矩阵,每一件家具可以看成一个矩形,他们放置在秋实大哥的家里,相互之间没有重叠. 现在秋实大哥购置了一个新的大小为1×M的家具,秋实大

UESTC_秋实大哥下棋 2015 UESTC Training for Data Structures&lt;Problem I&gt;

I - 秋实大哥下棋 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 胜负胸中料已明,又从堂上出奇兵.秋实大哥是一个下棋好手,独孤求败的他觉得下棋已经无法满足他了,他开始研究一种新的玩法. 在一个n×m的棋盘上,放置了k个车,并且他在棋盘上标出了q个矩形,表示矩形内部是战略要地. 秋实大哥要求一个矩形内的每一个格子,都至少能被一辆在矩形内的车攻击到,

UESTC_秋实大哥与花 2015 UESTC Training for Data Structures&lt;Problem B&gt;

B - 秋实大哥与花 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit Status 秋实大哥是一个儒雅之人,昼听笙歌夜醉眠,若非月下即花前. 所以秋实大哥精心照料了很多花朵.现在所有的花朵排成了一行,每朵花有一个愉悦值. 秋实大哥每天要对着某一段连续的花朵歌唱,然后这些花朵的愉悦值都会增加一个相同的值v(v可能为负). 同时他想知道每次他唱完歌后这一段连续的花朵的