2823. Sliding
Window
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 |
[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 |
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.
8 3
1 3 -1 -3 5 3 6 7
Sample Output
-1 -3 -3 -3 3 3
3 3 5 5
6 7
î÷åðåäü – ñêîëüçÿùåå îêíî
Ïðè ïîìîùè î÷åðåäè, êîòîðàÿ
ïîääåðæèâàåò ìèíèìóì è ìàêñèìóì, ìîäåëèðóåì ñêîëüçÿùåå îêíî.
Èñïîëüçîâàíèå ñòðóêòóðû deque èç STL äàåò Time Limit.
Ñëåäóåò ðåàëèçîâàòü î÷åðåäü ïðè ïîìîùè ñïèñêà íà óêàçàòåëÿõ èëè ïðîìîäåëèðîâàòü åå íà ìàññèâå.
Ðåàëèçàöèÿ
àëãîðèòìà
#include <stdio.h>
int n, k, i, x;
class Deque
{
private:
int *value,
*min, *max;
int Bvalue,
Evalue, Bmin, Emin, Bmax, Emax;
public:
Deque(int n =
1000010)
{
value = new
int[n];
min = new int[n];
max = new int[n];
Bvalue = Evalue = Bmin = Emin = Bmax = Emax
= 0;
}
~Deque()
{
delete[]
value;
delete[]
min;
delete[]
max;
}
void pop(void)
{
if (Bvalue
!= Evalue)
{
if
(value[Bvalue] == min[Bmin]) Bmin++;
if
(value[Bvalue] == max[Bmax]) Bmax++;
Bvalue++;
}
}
int getMin(void)
{
return
(Bmin != Emin) ? min[Bmin] : 0;
}
int getMax(void)
{
return
(Bmax != Emax) ? max[Bmax] : 0;
}
void push(int x)
{
value[Evalue++] = x;
while((Bmin
!= Emin) && (x < min[Emin-1])) Emin--;
while((Bmax
!= Emax) && (x > max[Emax-1])) Emax--;
min[Emin++] = x;
max[Emax++] = x;
}
};
int main(void)
{
scanf("%d
%d",&n,&k);
Deque d(n);
for(i = 1; i
<= k; i++)
{
scanf("%d",&x);
d.push(x);
}
int *minRes,
*maxRes;
minRes = new int[n - k + 1];
maxRes = new int[n - k + 1];
for(; i <=
n; i++)
{
minRes[i-k-1] = d.getMin();
maxRes[i-k-1] = d.getMax();
scanf("%d",&x);
d.pop();
d.push(x);
}
minRes[i-k-1] = d.getMin();
maxRes[i-k-1] = d.getMax();
for(i = 0; i
<= n - k; i++)
printf("%d
",minRes[i]);
printf("\n");
for(i = 0; i
<= n - k; i++)
printf("%d
",maxRes[i]);
printf("\n");
delete[] minRes;
delete[]
maxRes;
return 0;
}
Ðåàëèçàöèÿ
àëãîðèòìà – STL deque, TLE
#include <cstdio>
#include <deque>
#include <vector>
using namespace
std;
int n, k, i, x;
vector<int> minRes, maxRes;
class Deque
{
private:
deque<int>
q, min, max;
public:
void pop(void)
{
if
(!q.empty())
{
if
(q.front() == min.front()) min.pop_front();
if
(q.front() == max.front()) max.pop_front();
q.pop_front();
}
}
int getMin(void)
{
return
(min.empty()) ? 0 : min.front();
}
int getMax(void)
{
return (max.empty())
? 0 : max.front();
}
void push(int x)
{
q.push_back(x);
while(!min.empty()
&& x < min.back()) min.pop_back();
while(!max.empty()
&& x > max.back()) max.pop_back();
min.push_back(x);
max.push_back(x);
}
};
int main(void)
{
scanf("%d
%d",&n,&k);
Deque d;
for(i = 1; i
<= k; i++)
{
scanf("%d",&x);
d.push(x);
}
for(; i <=
n; i++)
{
minRes.push_back(d.getMin());
maxRes.push_back(d.getMax());
scanf("%d",&x);
d.pop();
d.push(x);
}
minRes.push_back(d.getMin());
maxRes.push_back(d.getMax());
for(i = 0; i
< minRes.size(); i++)
printf("%d
",minRes[i]);
printf("\n");
for(i = 0; i
< maxRes.size(); i++)
printf("%d
",maxRes[i]);
printf("\n");
return 0;
}