Ankit_132

B

Oct 22nd, 2023
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define ll     long long
  7. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  8.  
  9. int main()
  10. {
  11.     _test
  12.     {
  13.         int n;
  14.         string s;
  15.         cin>>n>>s;
  16.  
  17.         set<int> z;
  18.         for(int i=0; i<n; i++)
  19.         {
  20.             if(s[i] == '0')
  21.                 z.insert(i);
  22.         }
  23.  
  24.         ll int ans = 0;
  25.  
  26.         for(int i=n-1; i>=0; i--)
  27.         {
  28.             if(s[i] == '1')
  29.             {
  30.                 if(ans==-1 || !z.size())        ans = -1;
  31.                 else
  32.                 {
  33.                     ans += i - *z.rbegin();
  34.                     s[*z.rbegin()] = '1';
  35.                     z.erase(--z.end());
  36.                 }
  37.             }
  38.             else if(z.size() && *z.rbegin()==i)
  39.                 z.erase(i);
  40.  
  41.             cout<<ans<<" ";
  42.         }
  43.         cout<<"\n";
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment