#2 WA 了 90分 有dalao看看吗 代码如下

P1886 滑动窗口 /【模板】单调队列

Obtuse_Angle @ 2021-08-03 17:11:08

#include <iostream>
#include <queue>
#include <vector>

using namespace std ;
const int MAXN = 1e6 + 10 ;
int n , k , head = 1 , tail ;
int a[MAXN] , de[MAXN] ;
int main()
{
    cin >> n >> k ;
    for ( int i = 1 ; i <= n ; i ++ )
    {
        cin >> a[i] ;
    }
    for ( int i = 1 ; i <= n ; i ++ )
    {
        while(head<=tail&&de[head] <= i - k ) head ++ ;
        while(head<=tail&&a[de[tail]]>=a[i]) tail -- ; // 如果头部<=尾部且进来的数小于尾部数字 
        de[++tail] = i ;
        if ( i >= k )
        {
            cout << a[de[head]] << " "; 
        }   
    }
    cout << endl ;
    for ( int i = 1 ; i <= n ; i ++ )
    {
        while(head<=tail&&de[head] <= i - k ) head ++ ;
        while(head<=tail&&a[de[tail]]<=a[i]) tail -- ; // 如果头部<=尾部且进来的数大于尾部数字 
        de[++tail] = i ;
        if ( i >= k )
        {
            cout << a[de[head]] << " "; 
        }   
    }

    return 0 ;
}

by Obtuse_Angle @ 2021-08-03 17:19:50

注释打得有点问题将就着看吧懒得重发


by Fan_Tuan @ 2021-08-03 17:20:42

你队列在求最大值的时候的时候没清空


by Fan_Tuan @ 2021-08-03 17:21:34

@BaiBai_250


by Obtuse_Angle @ 2021-08-03 17:22:49

@Fan_Tuan 好的谢谢了! 好人一生平胸


by Fan_Tuan @ 2021-08-03 17:29:04

@BaiBai_250 你直接 head = 1,tail = 0 就好了啊,不需要循环啊


by Obtuse_Angle @ 2021-08-03 17:33:25

@Fan_Tuan 我太


|