The main effect of the topic:
Give a sequence
X1 = 1
X2 = 2
X3 = 3
Xi = (Xi-1 + Xi-2 + Xi-3)% M + 1 for i = 4 to N
To find a shortest sequence of successive sequences, so that this subsequence contains a positive integer "1,k"
Ideas:
Scan it again, using a queue to record the position of the number within the "1, k" interval, and then use a variable count to maintain the number of 1,k in "". When Count equals K, the current sequence already satisfies the requirement, and the number of queue headers is the starting position.
Algorithm complexity O (n)
Code:
/* UVa 11536-smallest sub-array * */#include <iostream> #include <cstdio> #include <cstr
ing> #include <utility> #include <set> #include <queue> using namespace std;
typedef long long Int64;
typedef pair<int,int>pii;
const int INF = 0X3F3F3F3F;
const int MAXN = 1000010;
int n, m, K;
int ARR[MAXN];
int CNT[MAXN];
void Init () {arr[1] = 1;
ARR[2] = 2;
ARR[3] = 3;
for (int i=4; i<=n; ++i) arr[i] = (arr[i-1]+arr[i-2]+arr[i-3))%m + 1;
memset (CNT, 0, sizeof (CNT));
int main () {int ncase, Cas=1;
scanf ("%d", &ncase);
while (ncase--) {scanf ("%d%d%d", &n,&m,&k);
Init ();
int minx = INF;
int count=0;
queue<int>q; for (int i=1; i<=n; ++i) {if (arr[i]>=1 && arr[i]<=k) {Q.push (i);
if (cnt[arr[i]]++ = = 0) {++count;
while (count = = k) {int tmp = I-q.front () + 1;
Minx = MIN (tmp, Minx);
int val = Arr[q.front ()];
if (--cnt[val]==0) {--count;
} q.pop ();
} printf ("Case%d:", cas++);
if (minx = = INF) puts ("Sequence Nai");
else printf ("%d\n", Minx);
return 0; }
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/