poj 2823 Sliding Window

http://poj.org/problem?id=2823

优先队列

代码:

#include<iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

#include<string>

#include<vector>

#include<set>

#include<queue>

#include<stack>

#include<cmath>

#define LL long long



using namespace std;



const int N=1000005;

int a[N];

struct cmp1

{

    bool operator()(const int i,const int j)

    {

        return a[i]<a[j];

    }

};

struct cmp2

{

    bool operator()(const int i,const int j)

    {

        return a[i]>a[j];

    }

};

priority_queue<int,vector<int>,cmp1>q1;

priority_queue<int,vector<int>,cmp2>q2;

int ans1[N];

int ans2[N];

int main()

{

    //freopen("data.txt","r",stdin);

    int n,k;

    while(scanf("%d %d",&n,&k)!=EOF)

    {

        for(int i=1;i<=n;++i)

        scanf("%d",&a[i]);

        for(int i=1;i<k;++i)

        {

            q1.push(i);q2.push(i);

        }

        int I=0;

        for(int i=k;i<=n;++i,++I)

        {

            q1.push(i);q2.push(i);

            while(q1.top()<=i-k)

            q1.pop();

            while(q2.top()<=i-k)

            q2.pop();

            ans1[I]=a[q1.top()];

            ans2[I]=a[q2.top()];

        }

        for(int i=0;i<I;++i)

        {

            if(i)

            printf(" ");

            printf("%d",ans2[i]);

        }printf("\n");

        for(int i=0;i<I;++i)

        {

            if(i)

            printf(" ");

            printf("%d",ans1[i]);

        }printf("\n");

    }

    return 0;

}

  

你可能感兴趣的:(window)