-
Notifications
You must be signed in to change notification settings - Fork 1
/
GoodlandElectricity.cpp
executable file
·60 lines (49 loc) · 1.32 KB
/
GoodlandElectricity.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 <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
int k;
int last = -1;
int next = 0;
int result = 0;
bool possible = true;
cin >> n >> k ;
last = -k;
vector< int > input( n );
vector< int > ones;
for ( int i = 0 ; i < n ; ++i ){
cin >> input[ i ];
if( input[ i ] == 1 ) ones.push_back( i );
}
for( int i = 0; i < n; ++i ){
if( input[ i ] == 0 ){
if( abs( i - last ) >= k and abs( i - ones[ next ] ) >= k ){
possible = false;
}
}
else{
if( next == ones.size() - 1 ){
if( last + k - 1 >= n-1 ) result++;
else last = i;
}
else{
next++;
int firstScope = last + k - 1;
int nextScope = ones[ next ] - k +1;
if( firstScope >= nextScope - 1 ){
result++;
}
else
last = i;
}
}
}
if( possible ) cout << ones.size() - result << "\n";
else cout << "-1\n";
return 0;
}