P1886 30分 RE 蒟蒻求救

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

贞白徐晟 @ 2019-09-24 19:00:00

#include <cstdio>
#include <queue>
using namespace std;
int n, k;
int a[2000005], ans1[2000005], ans2[2000005], t1[2000005], t2[2000005];
priority_queue <int> q1;
priority_queue <int, vector <int>, greater <int> > q2;
int main ()
{
    scanf ("%d %d", &n, &k);
    for (int i = 1; i <= n; i++){
            scanf ("%d", &a[i]);
        if (i <= k){
            q1.push (a[i]);         
            q2.push (a[i]); 
        }
    }
    ans1[k] = q1.top ();
    ans2[k] = q2.top ();    
    for (int i = k + 1; i <= n; i++){
        t1[a[i - k]]++;
        t2[a[i - k]]++;
        q1.push (a[i]);
        q2.push (a[i]);
        while (!q1.empty () && t1[q1.top ()]){
            t1[q1.top ()]--; 
            q1.pop ();
        } 
        while (!q2.empty () && t2[q2.top ()]){
            t2[q2.top ()]--;
            q2.pop (); 
        }
        ans1[i] = q1.top ();
        ans2[i] = q2.top ();  
    }
    for (int i = k; i <= n; i++) printf ("%d ", ans2[i]);
    printf ("\n");
    for (int i = k; i <= n; i++) printf ("%d ", ans1[i]);   
    return 0;
}

by rikkidayo @ 2019-09-26 11:43:35

https://www.luogu.org/discuss/show/147419


|