2018 UESTC Training for Data Structures 好吃不过饺子

好吃不过饺子

单调队列维护区间最值(代码粗糙)

//27MS   3200KB
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <algorithm>
using namespace std;
const int MAX=1e5+5;
pair<int,int>A[MAX];
pair<int,int>Q[MAX];
int T,L;
void read(int &x)
{
    int f=1;x=0;char c=getchar();
    while (c>'9'||c<'0'){if(x=='-')f=-1;c=getchar();}
    while (c<='9'&&c>='0'){x=x*10+c-'0';c=getchar();}
    x*=f;
}
int main()
{
    int n,m,x,y,len;
    string a,b;
    read(n);read(m);
    for (int k=1;k<=n;k++)
    {
        read(x);read(y);
        A[k].first=x;
        A[k].second=y;
    }
    sort(A+1,A+n+1);
    for (int k=1;k<=m;k++)
    {
        cin>>a>>b;
        read(len);
        T=0;L=-1;
        int ans=0,sum=0;
        memset(Q,0,sizeof(Q));
        if (a=="gt")
        {
            if (b=="min")
            {
                for (int i=1;i<=n;i++)
                {
                    while (A[i].first-Q[T].first>len&&L>=T) T++;
                    if (L<T)
                    {
                        Q[++L].first=A[i].first;
                        Q[L].second=A[i].second;
                        continue;
                    }
                    if (A[i].second>Q[T].second) ans++;
                    while (Q[L].second>A[i].second&&L>=T) L--;
                    Q[++L].first=A[i].first;
                    Q[L].second=A[i].second;
                }
            }
            else if (b=="max")
            {
                for (int i=1;i<=n;i++)
                {
                    while (A[i].first-Q[T].first>len&&L>=T) T++;
                    if (L<T)
                    {
                        Q[++L].first=A[i].first;
                        Q[L].second=A[i].second;
                        continue;
                    }
                    if (A[i].second>Q[T].second) ans++;
                    while (Q[L].second<A[i].second&&L>=T) L--;
                    Q[++L].first=A[i].first;
                    Q[L].second=A[i].second;
                }
            }
            else
            {
                for (int i=1;i<=n;i++)
                {
                    while (A[i].first-Q[T].first>len&&L>=T) {sum-=Q[T].second;T++;}
                    if (L<T)
                    {
                        Q[++L].first=A[i].first;
                        Q[L].second=A[i].second;
                        sum+=A[i].second;
                        continue;
                    }
                    if (A[i].second>=(sum/(L-T+1)+1)) ans++;
                    Q[++L].first=A[i].first;
                    Q[L].second=A[i].second;
                    sum+=A[i].second;
                }
            }
        }
        else
        {
            if (b=="min")
            {
                for (int i=1;i<=n;i++)
                {
                    while (A[i].first-Q[T].first>len&&L>=T) T++;
                    if (L<T)
                    {
                        Q[++L].first=A[i].first;
                        Q[L].second=A[i].second;
                        continue;
                    }
                    if (A[i].second<Q[T].second) ans++;
                    while (Q[L].second>A[i].second&&L>=T) L--;
                    Q[++L].first=A[i].first;
                    Q[L].second=A[i].second;
                }
            }
            else if (b=="max")
            {
                for (int i=1;i<=n;i++)
                {
                    while (A[i].first-Q[T].first>len&&L>=T) T++;
                    if (L<T)
                    {
                        Q[++L].first=A[i].first;
                        Q[L].second=A[i].second;
                        continue;
                    }
                    if (A[i].second<Q[T].second) ans++;
                    while (Q[L].second<A[i].second&&L>=T) L--;
                    Q[++L].first=A[i].first;
                    Q[L].second=A[i].second;
                }
            }
            else
            {
                for (int i=1;i<=n;i++)
                {
                    while (A[i].first-Q[T].first>len&&L>=T) {sum-=Q[T].second;T++;}
                    if (L<T)
                    {
                        Q[++L].first=A[i].first;
                        Q[L].second=A[i].second;
                        sum+=A[i].second;
                        continue;
                    }
                    if (A[i].second<=sum/(L-T+1)) ans++;
                    Q[++L].first=A[i].first;
                    Q[L].second=A[i].second;
                    sum+=A[i].second;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/nrtostp/article/details/80420808