1717: [Usaco2006 Dec]Milk Patterns 产奶的模式
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 952 Solved: 519
[ Submit][ Status][ Discuss]
Description
农夫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。
Input
* Line 1: 两个整数 N,K。
* Lines 2..N+1: 每行一个整数表示当天的质量值。
Output
* Line 1: 一个整数:N天中最长的出现了至少K次的模式的长度
Sample Input
8 2
1
2
3
2
3
2
3
1
1
2
3
2
3
2
3
1
Sample Output
4
HINT
Source
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = 2E4 + 20;
int n,T,cur = 1,a[maxn],c[maxn*100],t[maxn]
,t2[maxn],sa[maxn],rank[maxn],height[maxn];
void Getsa()
{
int *x = t,*y = t2,m = 1000000;
for (int i = 1; i <= n; i++) ++c[x[i] = a[i]];
for (int i = 2; i <= m; i++) c[i] += c[i-1];
for (int i = n; i; i--) sa[c[x[i]]--] = i;
for (int k = 1; k < n; k <<= 1) {
int p = 0;
for (int i = n; i > n - k; i--) y[++p] = i;
for (int i = 1; i <= n; i++) if (sa[i] - k > 0) y[++p] = sa[i] - k;
for (int i = 1; i <= m; i++) c[i] = 0;
for (int i = 1; i <= n; i++) ++c[x[y[i]]];
for (int i = 2; i <= m; i++) c[i] += c[i-1];
for (int i = n; i; i--) sa[c[x[y[i]]]--] = y[i];
p = 1; swap(x,y); x[sa[1]] = 1;
for (int i = 2; i <= n; i++)
x[sa[i]] = y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k]?p:++p;
if (p >= n) return;
m = p;
}
}
void Rank_and_Height()
{
for (int i = 1; i <= n; i++) rank[sa[i]] = i;
int k = 0;
for (int i = 1; i <= n; i++) {
if (k) --k;
int j = sa[rank[i] - 1];
while (a[i+k] == a[j+k]) ++k;
height[rank[i]] = k;
}
}
bool Judge(int now)
{
int tot = 0;
for (int i = 1; i <= n; i++) {
if (height[i] < now) {
if (tot + 1 >= T) return 1;
tot = 0; continue;
}
++tot;
}
return tot + 1 >= T;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n >> T;
for (int i = 1; i <= n; i++) scanf("%d",&a[i]);
Getsa();
Rank_and_Height();
int L,R; L = 0,R = n;
while (R - L > 1) {
int mid = (L + R) >> 1;
if (Judge(mid)) L = mid;
else R = mid;
}
if (Judge(R)) cout << R;
else cout << L;
return 0;
}