Mehulcoder

Cure Fit Bulb problem

Oct 6th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. /*
  2. Author: Mehul Chaturvedi
  3. Talent is overrated
  4. */
  5.  
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8.  
  9. using ll = long long;
  10. #define vll vector<long long>
  11. #define INF 4557430888798830399ll
  12. #define rep(i, n) for (int i = 0, _n = (n); i < _n; i++)
  13.  
  14. // Find closest group of k+1 zeroes ---> Ones in our new array
  15.  
  16. void solve() {
  17.     ll n, k; cin >> n >> k;
  18.     string s; cin >> s;
  19.  
  20.     vector<ll> v(n);
  21.     rep(i, n) v[i] = !(s[i] - '0');
  22.  
  23.     vll pre(n, 0);
  24.     rep(i, n) {
  25.         pre[i] = v[i];
  26.         if (i != 0) pre[i] += pre[i - 1];
  27.     }
  28.  
  29.     map<ll, ll> m;
  30.     ll best = INF;
  31.     rep(i, n) {
  32.         if (v[i] == 1) {
  33.             m[pre[i]] = i;
  34.             if (m.find(pre[i] - k) != m.end()) {
  35.                 best = min(best, i - m[pre[i] - k] + 1);
  36.             }
  37.         }
  38.     }
  39.  
  40.     cout << best - 2 << '\n';
  41.     return;
  42. }
  43.  
  44. int main(int argc , char ** argv) {
  45.     ios_base::sync_with_stdio(false) ;
  46.     cin.tie(NULL) ;
  47.  
  48.     ll t = 1;
  49.     while (t--) {
  50.         solve();
  51.     }
  52.  
  53.     return 0 ;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment