一个区间移动的问题,1A了,感觉没什么好说的。。
13975926 | 11536 | Smallest Sub-Array | Accepted | C++ | 0.809 | 2014-08-01 11:00:20 |
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define INF 1 << 30
#define MAXD 1000000 + 10
#define LEN 100 + 10
int array[MAXD];
int vis[LEN];
int N,M,K;
void init(){
memset(vis,0,sizeof(vis));
array[1] = 1;
array[2] = 2;
array[3] = 3;
for(int i = 4 ; i <= N ; i++)
array[i] = (array[i - 1] + array[i - 2] + array[i - 3]) % M + 1;
return ;
}
int main(){
int T,Case = 1;
scanf("%d",&T);
while(T--){
int cnt = 0;
int ok = 0;
int ans = INF;
scanf("%d%d%d",&N,&M,&K);
init();
int l = 1 , r = 3;
vis[1] = 1;
vis[2] = 1;
cnt = 2;
if(K == 1){
ans = 1;
}
else if(K == 2){
ans = 2;
}
while(r <= N){
int t = array[r]; /*右指针移动到的值*/
if(t <= K && !vis[t]){ /*这个值在[1,k]并且没有访问过*/
vis[t]++;
cnt ++;
}
else if(t <= K && vis[t]){
vis[t]++;
int i;
for(i = l ; i <= r ; i ++){ /*左指针移动*/
int t = array[i];
if(t <= K){
if(vis[t] > 1)
vis[t]--;
else
break;
}
}
l = i;
}
if(cnt == K)
ans = min(ans,r - l + 1);
r ++;
}
if(ans < INF)
printf("Case %d: %d\n",Case ++,ans);
else
printf("Case %d: sequence nai\n",Case++);
}
return 0;
}