luogu_P2852 [USACO06DEC]牛奶模式Milk Patterns

二分答案+哈希

#include<iostream>
#include<cstdio>

#define ri register int
#define u unsigned long long

namespace opt {

    inline u in() {
        u x(0),f(1);
        char s(getchar());
        while(s<'0'||s>'9') {
            if(s=='-') f=-1;
            s=getchar();
        }
        while(s>='0'&&s<='9') {
            x=(x<<1)+(x<<3)+s-'0';
            s=getchar();
        }
        return x*f;
    }

}

using opt::in;

#define NN 20005

#include<algorithm>

namespace mainstay {

    const u bas=131;

    u N,K,a[NN],t[NN],pow[NN]= {1};

    u check(const u &x) {
        u _n(0),now(1),_re(1);
        for(ri l(1),r(l+x-1); r<=N; ++l,++r) t[++_n]=a[r]-a[l-1]*pow[r-l+1];
        std::sort(t+1,t+_n+1);
        for(ri i(2); i<=_n; ++i) {
            if(t[i]^t[i-1]) now=1;
            else ++now;
            _re=std::max(_re,now);
        }
        return _re>=K;
    }

    inline void solve() {
        N=in(),K=in();
        u _a,l(1),r(N),ans(0);
        for(ri i(1); i<=N; ++i) _a=in(),a[i]=a[i-1]*bas+_a,pow[i]=pow[i-1]*bas;
        while(l<=r) {
            u mid((l+r)>>1);
            if(check(mid)) ans=mid,l=mid+1;
            else r=mid-1;
        }
        printf("%d",ans);
    }

}

int main() {

    //freopen("pattern.in","r",stdin);
    //freopen("pattern.out","w",stdout);
    mainstay::solve();

}

 

posted @ 2019-11-11 18:59  pai_hoo  阅读(113)  评论(0编辑  收藏  举报