/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 2ms 540.0 KiB
#3 Accepted 2ms 508.0 KiB
#4 Accepted 1ms 560.0 KiB
#5 Accepted 1ms 540.0 KiB

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
Submission
Problem
P1016 Swap sort
Language
C++20 (G++ 13.2.0)
Submit At
2024-01-05 16:18:22
Judged At
2024-11-11 03:44:12
Judged By
Score
100
Total Time
2ms
Peak Memory
560.0 KiB