fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6. inline int power(int a, int b) {
  7. int x = 1;
  8. while (b) {
  9. if (b & 1) x *= a;
  10. a *= a;
  11. b >>= 1;
  12. }
  13. return x;
  14. }
  15.  
  16.  
  17. const int M = 1000000007;
  18. const int N = 3e5+9;
  19. const int INF = 2e9+1;
  20. const int LINF = 2000000000000000001;
  21.  
  22. //_ ***************************** START Below *******************************
  23.  
  24. string a;
  25.  
  26. int consistency(int n, int k){
  27.  
  28. vector<int> zeroes;
  29. for(int i=0; i<n; i++){
  30. if(a[i] == '0') zeroes.push_back(i+1);
  31. }
  32.  
  33. int s = 0, e = 0;
  34. int l = 0;
  35. int ans = INF;
  36.  
  37. while(e<zeroes.size()){
  38.  
  39.  
  40. if(e-s+1 < k+1){
  41. e++;
  42. }
  43. else{
  44.  
  45. while(l<e ){
  46. int curr = max(zeroes[l]-zeroes[s], zeroes[e]-zeroes[l]);
  47. int next = max(zeroes[l+1]-zeroes[s], zeroes[e]-zeroes[l+1]);
  48.  
  49. if(next > curr) break;
  50. l++;
  51. }
  52. int maxDist = max(zeroes[l]-zeroes[s], zeroes[e]-zeroes[l]);
  53.  
  54. ans = min(ans, maxDist);
  55. s++;
  56. e++;
  57. }
  58. }
  59.  
  60. return ans;
  61. }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. void solve() {
  78.  
  79. int n, k;
  80. cin>>n >> k;
  81.  
  82. cin >> a;
  83. cout << consistency(n, k) << endl;
  84.  
  85.  
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92. int32_t main() {
  93. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  94.  
  95. int t = 1;
  96. // cin >> t;
  97. while (t--) {
  98. solve();
  99. }
  100.  
  101. return 0;
  102. }
Success #stdin #stdout 0s 5316KB
stdin
7 2
0100100
stdout
2