Submission #69911857


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

/////////////////// メイン ///////////////////

int main () {
  
  /////////////////// 前入力 ///////////////////
  
  int q;
  cin >> q;

  /////////////////// 前処理 ///////////////////



  /////////////////// ループ ///////////////////

  for (int loop=0; loop<q; loop++) {
    
    //////////////////// 入力 ////////////////////

    int n;
    string s;
    cin >> n >> s;

    //////////////// 出力変数定義 ////////////////

    int result = 0;

    //////////////////// 処理 ////////////////////

    // 0と1それぞれの個数
    int count0 = 0;
    int count1 = 0;

    // 0と1それぞれの最長記録
    int longest0 = 0;
    int longest1 = 0;

    // 0と1それぞれの現在の連続記録
    int tmp0 = 0;
    int tmp1 = 0;

    // 最長記録を調査
    for (int i=0; i<n; i++) {
      if (s.at(i)=='0') {
        count0++;
        tmp0++;
        tmp1 = 0;
        longest0 = max(longest0,tmp0);
      } else {
        count1++;
        tmp1++;
        tmp0 = 0;
        longest1 = max(longest1,tmp1);
      }
    }

    // 最長区間以外の最終文字は2回ずつ、そうじゃないやつは1回ずつの操作が必要
    // 0に揃えるのと1に揃えるので小さい方が答え
    result = min((count0-longest0)*2+count1,(count1-longest1)*2+count0);

    //////////////////// 出力 ////////////////////

    cout << result << endl;
    
  }

  /////////////////// 後処理 ///////////////////



  //////////////////// 終了 ////////////////////

  return 0;

}

Submission Info

Submission Time
Task D - Pop and Insert
User wightou
Language C++ 23 (gcc 12.2)
Score 400
Code Size 1660 Byte
Status AC
Exec Time 53 ms
Memory 4244 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 3568 KiB
01_small_00.txt AC 53 ms 3528 KiB
02_random_00.txt AC 7 ms 3444 KiB
02_random_01.txt AC 7 ms 3468 KiB
02_random_02.txt AC 7 ms 3652 KiB
02_random_03.txt AC 7 ms 4076 KiB
02_random_04.txt AC 8 ms 4148 KiB
02_random_05.txt AC 8 ms 4244 KiB
02_random_06.txt AC 7 ms 4152 KiB
03_handmade_00.txt AC 7 ms 4144 KiB
03_handmade_01.txt AC 7 ms 4140 KiB
03_handmade_02.txt AC 7 ms 4188 KiB
03_handmade_03.txt AC 7 ms 4236 KiB
03_handmade_04.txt AC 7 ms 4224 KiB