【求助】为什么for从1开始过不了,从0开始可以
作者:
Kitesxian
,
2024-05-27 17:19:52
,
所有人可见
,
阅读 5
//https://www.acwing.com/problem/content/description/156/
#include<iostream>
#include<deque>
#include<algorithm>
using namespace std;
const int N = 1e6 + 10;
int a[N], q[N];
int n, k;
int main()
{
cin >> n >> k;
// for (int i = 1; i <= n;i++)
for(int i = 0;i<n;i++)
scanf("%d", &a[i]);
int hh = 0, tt = -1;
// for (int i = 1;i<=n;i++)
for(int i = 0;i<n;i++)
{
if(hh<=tt&&q[hh]<i-k+1)
hh++;
while(hh<=tt&&a[q[tt]]>=a[i])
tt--;
q[++tt] = i;
if(i>=k-1)
printf("%d ", a[q[hh]]);
}
puts("");
hh = 0, tt = -1;
// for (int i = 1; i <= n;i++)
for(int i = 0;i<n;i++)
{
if(hh<=tt&&q[hh]<i-k+1)
hh++;
while(hh<=tt&&a[q[tt]]<=a[i])
--tt;
q[++tt] = i;
if (i >= k - 1)
printf("%d ", a[q[hh]]);
}
puts("");
return 0;
}
//https://www.acwing.com/problem/content/description/156/