/ SeriousOJ /

Record Detail

Accepted


  

Code

#include<bits/stdc++.h>
using namespace std;
const long long M=2e5+10,MOD=1000000007;
typedef long long ll;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1;
    cin>>t;
    while(t--){
    int n;
    cin>>n;
    string s;
    cin>>s;
    int x=1;
    int y=1;
    for(int i=1;i<n;i++)x&=(s[i]>=s[i-1]);
    for(int i=1;i<n;i++)y&=(s[i]<=s[i-1]);
    if(x || y){
        cout<<0<<"\n";
        continue;
    }
    vector<int>N(2,0);
    for(int i=0;i<n;i++){
        N[s[i]-'0']++;
    }
   
    for(int i=0;i<N[0];i++){
        x+=(s[i]=='1');
    }
    for(int i=n-1;i>=n-N[0];i--){
        y+=(s[i]=='1');
    }
   int mx=min(x,y);
   x=0,y=0;
   for(int i=0;i<N[1];i++){
    x+=(s[i]=='0');
   }
   for(int i=n-1;i>=n-N[1];i--)y+=(s[i]=='0');
   cout<<min({mx,x,y})<<"\n";


     
 }


    
    return 0;
 
}

Information

Submit By
Type
Pretest
Problem
P1016 Swap sort
Language
C++20 (G++ 13.2.0)
Submit At
2024-01-05 16:18:12
Judged At
2024-11-11 03:44:12
Judged By
Score
0
Total Time
0ms
Peak Memory
0 Bytes