-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa 11536 - Smallest Sub-Array.cc
71 lines (60 loc) · 1.42 KB
/
UVa 11536 - Smallest Sub-Array.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010, INF = (int) 1e9;
int a[N], cnt[N];
int main(){
int T;
scanf("%d", &T);
int c = 0;
while(T--){
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
a[1] = 1;
a[2] = 2;
a[3] = 3;
for(int i = 4; i <= n; i++){
a[i] = (a[i - 1] + a[i - 2] + a[i - 3]) % m + 1;
}
memset(cnt, 0, sizeof cnt);
int ans = INF;
for(int i = 1; i <= 3; i++){
cnt[a[i]]++;
bool valid = true;
for(int j = 1; j <= k; j++){
if(!cnt[j]){
valid = false;
break;
}
}
if(valid){
ans = i;
break;
}
}
l = 1;
r = 3;
while(r <= n){
bool valid = true;
for(int j = 1; j <= k; j++){
if(!cnt[j]){
valid = false;
break;
}
}
if(valid){
ans = min(ans, r - l + 1);
cnt[a[l]]--;
l++;
} else {
r++;
cnt[a[r]]++;
}
}
printf("Case %d: ", ++c);
if(ans == INF){
puts("sequence nai");
} else {
printf("%d\n", ans);
}
}
}