1717: [Usaco2006 dec]milk Patterns Milk production modelTime Limit:5 Sec Memory limit:64 MB
submit:952 solved:519
[Submit] [Status] [Discuss]
Description
Farmer John found that the quality of his cows ' milk had been changing. After careful investigation, he found that although he could not foresee the quality of milk production tomorrow, there was a lot of overlap in quality for several consecutive days. We call it a "pattern." John's milk can be given a number between 0 and 1000000 by quality. And John records the value of the milk quality of N (1<=n<=20000) days. He wanted to know the length of the longest pattern that appeared at least K (2<=k<=n). For example, 1 2 3 2 3 2 3, 1 2 3 2 appeared 3 times. When k=2, this length is 4. Input
* Line 1: two integer n,k.
* Lines 2..n+1: One integer per line represents the quality value of the day. Output
* Line 1: An integer: N days the longest occurrence of at least K mode of length Sample Input 8 2
1
2
3
2
3
2
3
1
Sample Output 4
HINT
Source
Gold
[submit][status][discuss]
This is the template for the suffix array ... The SA, rank, and height array all appear with to construct a first-suffix array binary ans,,, On the height array, the longest common prefix for any two suffixes is the min of all the height between them. So, if it exists, it should be a series of, greedy and blind. Bulabulabula
#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;
}