luogu2852 [USACO06DEC]牛奶模式Milk Patterns

(http://www.elijahqi.win/2017/07/14/luogu2852-usaco0%E2%80%A6%E5%A5%B6%E6%A8%A1%E5%BC%8Fmilk-patterns/)
题目描述

Farmer John has noticed that the quality of milk given by his cows varies from day to day. On further investigation, he discovered that although he can’t predict the quality of milk from one day to the next, there are some regular patterns in the daily milk quality.

To perform a rigorous study, he has invented a complex classification scheme by which each milk sample is recorded as an integer between 0 and 1,000,000 inclusive, and has recorded data from a single cow over N (1 ≤ N ≤ 20,000) days. He wishes to find the longest pattern of samples which repeats identically at least K (2 ≤ K ≤ N) times. This may include overlapping patterns – 1 2 3 2 3 2 3 1 repeats 2 3 2 3 twice, for example.

Help Farmer John by finding the longest repeating subsequence in the sequence of samples. It is guaranteed that at least one subsequence is repeated at least K times.

农夫John发现他的奶牛产奶的质量一直在变动。经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠。我们称之为一个“模式”。 John的牛奶按质量可以被赋予一个0到1000000之间的数。并且John记录了N(1<=N<=20000)天的牛奶质量值。他想知道最长的出现了至少K(2<=K<=N)次的模式的长度。比如1 2 3 2 3 2 3 1 中 2 3 2 3出现了两次。当K=2时,这个长度为4。
输入输出格式

输入格式:

Line 1: Two space-separated integers: N and K

Lines 2..N+1: N integers, one per line, the quality of the milk on day i appears on the ith line.

输出格式:

Line 1: One integer, the length of the longest pattern which occurs at least K times
输入输出样例

输入样例#1:

8 2
1
2
3
2
3
2
3
1

输出样例#1:

4

数据可能经过弱化处理,仍然建议使用离散化才可以

一开始想用rmq,但是想不清,放弃,后来发现只需要扫一遍某一区间 长度差是否>=所给定的k,并且height满足我们二分的答案 同poj1743区别 poj1743要求不重复的重叠子串

#include<cstdio>
#include<map>
#include<cstring>
#define N 22000
#include<algorithm>
using namespace std;
inline int min1(int x,int y){
    return x<y?x:y;
}
inline int read(){
    int x=0;char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch<='9'&&ch>='0'){
        x=x*10+ch-'0';ch=getchar();
    }
    return x;
}
map<int,int> mm;
int a[N],a1[N],k,n,k1,rank0[N<<1],rank1[N],st[N],count1[N],tmp[N],sa[N],height[N],fmin1[N][200];
inline int query(int l,int r){
    int k=0;while ((1<<(k+1))<=(r-l+1))++k;
    return min(fmin1[l][k],fmin1[r-(1<<k)+1][k]);
}
bool check(int len){
    int l=2,r=l+k;
    while (r<=n){
        int tmp=query(l,r);
        if (tmp>=len) return true;
        ++l;++r;
    }
    return false;
}
inline bool judge(int len){
    int tmp=0;
    for (int i=1;i<=n;++i){
        if (height[i]>=len) {
            if (++tmp==k1-1) return true;
        }else tmp=0;
    }
    if (tmp>=k1-1)return true;else return false;
}
int main(){
    //freopen("2852.in","r",stdin);
    //freopen("2852.out","w",stdout);
    n=read();k1=read();
    for (int i=1;i<=n;++i) a[i]=read(),a1[i]=a[i];
    sort(a1+1,a1+n+1);
    for (int i=n;i>=1;--i) mm[a1[i]]=i;
    for (int i=1;i<=n;++i) a[i]=mm[a[i]]; 
//  for (int i=1;i<=n;++i) printf("%d ",a[i]);
    //printf("%d",mm.size());
    memset(rank0,0,sizeof(rank0));
    memset(rank1,0,sizeof(rank1));
    memset(st,0,sizeof(st));
    for (int i=1;i<=n;++i) st[a[i]]=1;
    for (int i=1;i<=n;++i) st[i]+=st[i-1];
    for (int i=1;i<=n;++i) rank0[i]=st[a[i]];
    k=0;
    for (int p=1;k!=n;p<<=1){
        memset(count1,0,sizeof(count1));
        for (int i=1;i<=n;++i) count1[rank0[i+p]]++;
        for (int i=1;i<=n;++i) count1[i]+=count1[i-1];
        for (int i=n;i>=1;--i) tmp[count1[rank0[i+p]]--]=i;
        memset(count1,0,sizeof(count1));
        for (int i=1;i<=n;++i) count1[rank0[i]]++;
        for (int i=1;i<=n;++i) count1[i]+=count1[i-1];
        for (int i=n;i>=1;--i) sa[count1[rank0[tmp[i]]]--]=tmp[i];
        memcpy(rank1,rank0,sizeof(rank0)>>1);
        rank0[sa[1]]=k=1;
        for (int i=2;i<=n;++i){
            if(rank1[sa[i-1]]!=rank1[sa[i]]||rank1[sa[i]+p]!=rank1[sa[i-1]+p]) ++k;
            rank0[sa[i]]=k;
        } 
    }
    k=0;
    for (int i=1;i<=n;++i){
        if (rank0[i]==1) {
            height[1]=0;continue;
        }
        k=k==0?0:k-1;
        while (a[i+k]==a[sa[rank0[i]-1]+k]) ++k;
        height[rank0[i]]=k;
    }
//init
    //for (int i=1;i<=n;++i) printf("%d ",height[i]);printf("\n");
    for (int i=1;i<=n;++i) fmin1[i][0]=height[i];
    for (int i=1;(1<<i)<=n-1;++i){
        for (int j=2;j+(1<<i)-1<=n;++j){
            fmin1[j][i]=min1(fmin1[j][i-1],fmin1[j+(1<<(i-1))][i-1]);
        }
    }
    int l=1,r=n,ans=0;
    while (l<r){
        int mid=(l+r)>>1;
        if (judge(mid)) ans=mid,l=mid+1;else r=mid;
    }
    printf("%d",ans);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值