选择客栈 NOIP2011提高组D1T2 单调队列 前缀和

代码很好懂,关键句需要自己琢磨,直接上代码了。

#include<cstdio>
using namespace std;
template<class T> inline void read(T &_a){
    bool f=0;int _ch=getchar();_a=0;
    while(_ch<'0' || _ch>'9'){if(_ch=='-')f=1;_ch=getchar();}
    while(_ch>='0' && _ch<='9'){_a=(_a<<1)+(_a<<3)+_ch-'0';_ch=getchar();}
    if(f)_a=-_a;
}

const int maxn=200001;
int n,k,p,col[maxn],cost[maxn],sum[maxn][50],last[50],tp;
long long ans;

int main()
{
    read(n); read(k); read(p);
    for(register int i=1;i<=n;++i)
    {
        read(col[i]);
        read(cost[i]);
        for (register int v=0;v<k;++v) sum[i][v]=sum[i-1][v]+(col[i]==v);
    }
        tp=1;
    while(cost[tp]>p) ++tp; 
    for (register int i=tp;i<=n;++i)
    {
        if(cost[i]<=p) tp=i;
        ans+=sum[last[col[i]]<=tp?i-1:tp][col[i]];
        last[col[i]]=i;
    }
    printf("%lld",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/jaywang/p/7736942.html