-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15565.cpp
60 lines (57 loc) · 803 Bytes
/
15565.cpp
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <math.h>
using namespace std;
int N, K;
int arr[1000010];
int main() {
cin >> N >> K;
for (int x = 1; x <= N; x++) {
cin >> arr[x];
}
int left = 1;
int right = 1;
int lion = 0;
int ans = 987654321;
while (left <= right && right <= N + 1) {
if (lion < K) {
if (arr[right] == 1) {
lion++;
right++;
}
else {
right++;
}
}
else if (lion > K) {
if (arr[left] == 1) {
lion--;
left++;
}
else {
left++;
}
}
else if (lion == K) {
if (ans > right - left) {
ans = right - left;
}
if (arr[left] == 1) {
lion--;
left++;
}
else {
left++;
}
}
}
if (ans == 987654321) {
cout << -1;
}
else {
cout << ans;
}
return 0;
}