Submission #69855293


Source Code Expand

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long

int main(){
	ios::sync_with_stdio(0); cin.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        string s;
        cin>>n>>s;
        s = "$" + s;
        
        int pre1[n+5], pre0[n+5], suf1[n+5], suf0[n+5];
        pre1[0] = pre0[0] = suf1[n+1] = suf0[n+1] = 0;

        for(int i=1; i<=n; i++){
            pre1[i] = pre1[i-1];
            pre0[i] = pre0[i-1];

            if(s[i] == '0'){
                pre0[i] += 2;
                pre1[i]++;
            }
            else{
                pre0[i]++;
                pre1[i] += 2;
            }
        }

        for(int i=n; i>=1; i--){
            suf1[i] = suf1[i+1];
            suf0[i] = suf0[i+1];

            if(s[i] == '0'){
                suf0[i] += 2;
                suf1[i]++;
            }
            else{
                suf0[i]++;
                suf1[i] += 2;
            }
        }

        int ans = 1e9;
        for(int i=1; i<=n; i++){
            if(s[i] != s[i-1]){
                int l = i;
                while(i+1 <= n && s[i+1] == s[i]) i++;

                // cout<<l<<' '<<i<<" ";
                if(s[l] == '1'){
                    ans = min(ans, pre1[l-1] + suf1[i+1]);
                    // cout<<pre1[l-1]<<" "<<suf1[i+1]<<endl;
                }
                else{
                    ans = min(ans, pre0[l-1] + suf0[i+1]);
                    // cout<<pre0[l-1]<<" "<<suf0[i+1]<<endl;
                }
            }
        }

        cout<<ans<<endl;
    }
	return 0;
}

Submission Info

Submission Time
Task D - Pop and Insert
User takeonicky
Language C++ 20 (gcc 12.2)
Score 400
Code Size 1650 Byte
Status AC
Exec Time 12 ms
Memory 11528 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 1
AC × 14
Set Name Test Cases
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 03_handmade_00.txt, 03_handmade_01.txt, 03_handmade_02.txt, 03_handmade_03.txt, 03_handmade_04.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3444 KiB
01_small_00.txt AC 12 ms 3652 KiB
02_random_00.txt AC 3 ms 3640 KiB
02_random_01.txt AC 3 ms 3848 KiB
02_random_02.txt AC 3 ms 5140 KiB
02_random_03.txt AC 6 ms 11388 KiB
02_random_04.txt AC 6 ms 11388 KiB
02_random_05.txt AC 8 ms 11424 KiB
02_random_06.txt AC 7 ms 11488 KiB
03_handmade_00.txt AC 6 ms 11468 KiB
03_handmade_01.txt AC 6 ms 11528 KiB
03_handmade_02.txt AC 7 ms 11488 KiB
03_handmade_03.txt AC 6 ms 11392 KiB
03_handmade_04.txt AC 6 ms 11392 KiB