UVA 11536 Smallest Sub-array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.