POJ 2823 Sliding Window(单调队列||线段树)

题意:

让你不停的查询区间长度为k的最小值和最大值
一开始用set模拟,发现常熟太大,T了,然后开始学单调队列,看了一会儿还是好理解的,所以就写了,发现G++会T,只能交C++,后来发现
有人线段树也能过,于是就写了一发,还真行,只不过就是吧查询的答案用全局变量来存,减少return的时间消耗,还是只能交C++。

单调队列:

//
//  Created by  CQU_CST_WuErli
//  Copyright (c) 2016 CQU_CST_WuErli. All rights reserved.
//
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <algorithm>
#include <sstream>
#define CLR(x) memset(x,0,sizeof(x))
#define OFF(x) memset(x,-1,sizeof(x))
#define MEM(x,a) memset((x),(a),sizeof(x))
#define BUG cout << "I am here" << endl
#define lookln(x) cout << #x << "=" << x << endl
#define SI(a) scanf("%d",&a)
#define SII(a,b) scanf("%d%d",&a,&b)
#define SIII(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define rep(flag,start,end) for(int flag=start;flag<=end;flag++)
#define Rep(flag,start,end) for(int flag=start;flag>=end;flag--)
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
#define Root 1,n,1
const int INF_INT=0x3f3f3f3f;
const long long INF_LL=0x7fffffff;
const int MOD=1e9+7;
typedef long long  ll;
using namespace std;

struct Sta
{
    int val,id;
    Sta(int val,int id):val(val),id(id){}
};

const int N=1000010;
int a[N];
int n,k;
int ans[N];

void getmin() {
    deque<Sta> q;
    rep(i,1,k-1) {
        while (q.size() && q[q.size()-1].val>=a[i]) q.pop_back();
        q.push_back(Sta(a[i],i));
    }
    rep(i,k,n) {
        while (q.size() && q.back().val>=a[i]) q.pop_back();
        q.push_back(Sta(a[i],i));
        while (q.size() && q.front().id<=i-k) q.pop_front();
        ans[i-k+1]=q.front().val;
    }
    rep(i,1,n-k+1) {
        printf("%d%c",ans[i],i<n-k+1?' ':'\n');
    }
}

void getmax() {
    deque<Sta> q;
    rep(i,1,k-1) {
        while (q.size() && q.back().val<=a[i]) q.pop_back();
        q.push_back(Sta(a[i],i));
    }
    rep(i,k,n) {
        while (q.size() && q.back().val<=a[i]) q.pop_back();
        q.push_back(Sta(a[i],i));
        while (q.size() && q.front().id<=i-k) q.pop_front();
        ans[i-k+1]=q.front().val;
    }
    rep(i,1,n-k+1) {
        printf("%d%c",ans[i],i<n-k+1?' ':'\n');
    }
}

int main(int argc, char const *argv[]) {
#ifdef LOCAL
    freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin);
    // freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout);
#endif
    while(SII(n,k)==2) {
        rep(i,1,n) SI(a[i]);
        getmin();
        getmax();
    }
    return 0;
}

线段树:

//
//  Created by  CQU_CST_WuErli
//  Copyright (c) 2016 CQU_CST_WuErli. All rights reserved.
//w
#include <stdio.h>
#define CLR(x) memset(x,0,sizeof(x))
#define OFF(x) memset(x,-1,sizeof(x))
#define MEM(x,a) memset((x),(a),sizeof(x))
#define BUG cout << "I am here" << endl
#define lookln(x) cout << #x << "=" << x << endl
#define SI(a) scanf("%d",&a)
#define SII(a,b) scanf("%d%d",&a,&b)
#define SIII(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define rep(flag,start,end) for(int flag=start;flag<=end;flag++)
#define Rep(flag,start,end) for(int flag=start;flag>=end;flag--)
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
#define Root 1,n,1
const int INF_INT=0x3f3f3f3f;
const long long INF_LL=0x7fffffff;
const int MOD=1e9+7;
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
const int N=1000010*4;

int n,k;

int ans1[1000010*4];
int ans2[1000010*4];

void pushup(int rt) {
    ans1[rt]=max(ans1[rt<<1],ans1[rt<<1|1]);
    ans2[rt]=min(ans2[rt<<1],ans2[rt<<1|1]);
}

void build(int l,int r,int rt) {
    int mid;
    if (l==r) {
        scanf("%d",ans1+rt);
        ans2[rt]=ans1[rt];
        return ;
    }
    mid=(l+r)>>1;
    build(Lson);
    build(Rson);
    pushup(rt);
}

int maxx;
void queryMax(int L,int R,int l,int r,int rt) {
    int mid;
    if (L<=l && r<=R) {
        maxx=max(maxx,ans1[rt]);
        return;
    }
    mid=(l+r)>>1;
    if (L<=mid) queryMax(L,R,Lson);
    if (R>mid) queryMax(L,R,Rson);
    return;
}

int minx;
void queryMin(int L,int R,int l,int r,int rt) {
    int mid;
    if (L<=l && r<=R) {
        minx=min(minx,ans2[rt]);
        return;
    }
    mid=(l+r)>>1;
    if (L<=mid) queryMin(L,R,Lson);
    if (R>mid) queryMin(L,R,Rson);
    return;
}

int main(int argc, char const *argv[]) {
#ifdef LOCAL
    freopen("C:\\Users\\john\\Desktop\\in.txt","r",stdin);
    // freopen("C:\\Users\\john\\Desktop\\out.txt","w",stdout);
#endif
    int i;
    while(SII(n,k)==2) {
        build(1,n,1);

        for(i=1;i<=n-k+1;i++) {
            minx=INF_INT;
            queryMin(i,i+k-1,1,n,1);
            printf("%d",minx);
            if (i<n-k+1) printf(" ");
            else printf("\n");
        }
        for(i=1;i<=n-k+1;i++) {
            maxx=-INF_INT;
            queryMax(i,i+k-1,1,n,1);
            printf("%d",maxx);
            if (i<n-k+1) printf(" ");
            else printf("\n");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值