Test instructions
X[i]= (X[i-3]+x[i-2]+x[i-1])%m+1, the shortest continuous subsequence of x, so that the subsequence contains a positive integer "1,k".
Analysis:
Scan once, use 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 distinct numbers in "1,k". When Count equals K, it is stated that the current sequence has satisfied the requirement, and the position of the number of the queue header is the starting position.
Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <utility>
#include <set>
#include <queue>
using namespace Std;
typedef pair<int,int>pii;
const int INF = 0X3F3F3F3F;
const int MAXN = 1000010;
int n,m,k;
int V[MAXN];
int CNT[MAXN];
int main ()
{
int T,cas=1;
scanf ("%d", &t);
while (t--)
{
scanf ("%d%d%d", &n,&m,&k);
V[1] = 1;
V[2] = 2;
V[3] = 3;
int i,j;
for (I=4;i<=n;++i)
V[i]= (v[i-1]+v[i-2]+v[i-3])%m+1;
memset (CNT, 0, sizeof (CNT));
int minx=inf;
int count=0;
queue<int>q;
for (I=1;i<=n;++i)
{
if (v[i]>=1&&v[i]<=k)
{
Q.push (i);
if (cnt[v[i]]++== 0)
{
++count;
}
while (COUNT==K)
{
int Tmp=i-q.front () +1;
Minx=min (Tmp,minx);
int Val=v[q.front ()];
if (--cnt[val]==0)
{
--count;
}
Q.pop ();
}
}
}
printf ("Case%d:", cas++);
if (minx = = INF)
printf ("Sequence nai\n");
Else
printf ("%d\n", Minx);
}
return 0;
}
UVA 11536 Smallest Sub-array