Created
June 13, 2021 15:01
-
-
Save ANtlord/1a9c1f41d131bf809fa098e071551b2c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
const int N = 3e5 + 5; | |
int n, k, p, prv[N]; | |
int main() { | |
// readining input | |
scanf("%d %d", &n, &k); | |
int last = -1, ans = 0; | |
for (int i = 0; i < n; i++) { | |
scanf("%d", &p); | |
if(p == 1) last = i; | |
prv[i] = last; | |
} | |
// end reading input | |
for (int i = 0; i < n;) { | |
int furthestPylon = prv[min(i + k - 1, n - 1)]; | |
if (furthestPylon == -1 || furthestPylon + k <= i) { | |
printf("-1\n"); | |
return 0; | |
} | |
i = furthestPylon + k; | |
ans++; | |
} | |
printf("%d\n", ans); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func pylons(k int32, cities []int32) int32 { | |
var ( | |
coverRight, count = -1, int32(0) | |
) | |
for cityCur := 0; cityCur < len(cities); cityCur++ { | |
if cities[cityCur] == 0 { | |
continue | |
} | |
lastStable := cityCur | |
potentialCityCur := cityCur | |
score := 0 | |
newRight := coverRight | |
for { | |
if potentialCityCur >= len(cities) { | |
cityCur = lastStable // <- possible optimization | |
break | |
} | |
if cities[potentialCityCur] == 0 { | |
potentialCityCur++ | |
continue | |
} | |
if left := potentialCityCur - int(k - 1); left > coverRight + 1 { | |
cityCur = lastStable | |
break | |
} | |
lastStable = potentialCityCur | |
newRight = lastStable + int(k - 1) | |
score = 1 | |
potentialCityCur++ | |
} | |
count += int32(score) | |
coverRight = newRight | |
if coverRight >= len(cities) - 1 { | |
break | |
} | |
} | |
if coverRight < len(cities) - 1 { | |
return -1 | |
} | |
return count | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment