Advertisement
TheAnshul

Sweet and Sour Rock

Jun 16th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. /***************************************************************************
  2.  * #######                    #                                            *
  3.  *    #     #    #  ######   # #    #    #   ####   #    #  #    #  #      *
  4.  *    #     #    #  #       #   #   ##   #  #       #    #  #    #  #      *
  5.  *    #     ######  #####  #     #  # #  #   ####   ######  #    #  #      *
  6.  *    #     #    #  #      #######  #  # #       #  #    #  #    #  #      *
  7.  *    #     #    #  #      #     #  #   ##  #    #  #    #  #    #  #      *
  8.  *    #     #    #  ###### #     #  #    #   ####   #    #   ####   ###### *
  9.  ***************************************************************************/
  10. #include<bits/stdc++.h>
  11. #define ll          long long
  12. #define pb          push_back
  13. #define endl        '\n'
  14. #define pii         pair<ll int,ll int>
  15. #define vi          vector<ll int>
  16. #define all(a)      (a).begin(),(a).end()
  17. #define F           first
  18. #define S           second
  19. #define sz(x)       (ll int)x.size()
  20. #define hell        1000000007
  21. #define rep(i,a,b)  for(ll int i=a;i<b;i++)
  22. #define lbnd        lower_bound
  23. #define ubnd        upper_bound
  24. #define bs          binary_search
  25. #define mp          make_pair
  26. using namespace std;
  27.  
  28. #define N  100005
  29.  
  30. int main()
  31. {
  32.     ios_base::sync_with_stdio(false);
  33.     cin.tie(0);
  34.     cout.tie(0);
  35.     int TESTS=1;
  36.     cin>>TESTS;
  37.     while(TESTS--)
  38.     {
  39.         ll n;
  40.         string s;
  41.         cin>>n;
  42.         cin>>s;
  43.         vi dp(n+1);
  44.         ll sum=0,mx=0,mn=0;
  45.         dp[0]=0;
  46.         rep(i,0,n)
  47.         {
  48.             if(s[i]=='0')
  49.                 sum--;
  50.             else
  51.                 sum++;
  52.             dp[i+1]=sum;
  53.             if(sum>mx)
  54.                 mx=sum;
  55.             if(sum<mn)
  56.                 mn=sum;
  57.         }
  58.         if(mn>=0)
  59.         {
  60.             cout<<n<<endl;
  61.             continue;
  62.         }
  63.         vi vdp(mx-mn+1,-2);
  64.         for(ll i=n;i>=0;i--)
  65.         {
  66.             if(vdp[dp[i]-mn]==-2)
  67.                 vdp[dp[i]-mn]=i;
  68.         }
  69.         ll fd=-1;
  70.         for(ll i=mx-mn;i>=0;i--)
  71.         {
  72.             if(vdp[i]>fd)
  73.                 fd=vdp[i];
  74.             vdp[i]=fd;
  75.         }
  76.         // rep(i,0,mx-mn+1)
  77.         // cout<<vdp[i]<<" ";
  78.         // cout<<endl;
  79.         ll val=0,i=0,ref,ans=0;
  80.         for(i=0;i<=n;i++)
  81.         {
  82.             val=dp[i];
  83.             // if(val==mx)
  84.             // {
  85.             //  continue;
  86.             // }
  87.             ref=vdp[val+1-mn];
  88.             if(ref>i)
  89.             {
  90.                 ans+=ref-i;
  91.                 i=ref;
  92.             }
  93.         }
  94.         cout<<ans<<endl;
  95.     }
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement