求m区间内的最小值

洛谷P1440 求m区间内的最小值

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

以上代表我此时的心情,调了一个小时。。。。只因为顺序,维护一个单调递增队列就好了,这里n很大,输出要优化,这才挽救了30分。。

#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[2000001],q[2000001];
int top,tai;

void Cin(int &x)
{
    char c=getchar();x=0;
    int y=1;
    while(c<'0'||c>'9')
    {
        if(c=='-')
        y=-1;
        c=getchar();
    }
    while(c<='9'&&c>='0')x=x*10+c-'0',c=getchar();
    x*=y;
}

void Cout(int x)
{
    if(x>9) Cout(x/10);
    putchar(x%10+'0');
}

int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++) Cin(a[i]);

    for(int i=1;i<=n;i++)
    {
        while(q[tai]-q[top]>=m||top==0 ) top++;
        Cout(a[q[top]]),putchar(10);
        while(top<=tai&&a[q[tai]]>=a[i]) tai--;
        tai++;
        q[tai]=i;
        
    }
    return 0;
}
原文地址:https://www.cnblogs.com/war1111/p/7299995.html