Submission #69853787


Source Code Expand

// Problem: 'D - Pop and Insert'
// Contest: 'AtCoder - AtCoder Beginner Contest 426'
// URL: 'https://atcoder.jp/contests/abc426/tasks/abc426_d'
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by competitive-companion.el (https://github.com/luishgh/competitive-companion.el)

#include <bits/stdc++.h>

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'

typedef long long ll;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

int n;
string s;

int res(char x) {
  int l = 0, r = n - 1;

  int ans = 0;
  int left = 0, right = 0;

  while (l <= r) {
    if (left == 0) {
      while (l < n and s[l] == x) {
        ans++;
        l++;
      }
      while (l < n and s[l] != x) left++, l++;
    }
    if (right == 0) {
      while (r >= 0 and s[r] == x) {
        ans++;
        r--;
      }
      while (r >= 0 and s[r] != x) right++, r--;
    }
    if (l > r) break;
    assert(s[l] == x and s[r] == x);
    if (left <= right) {
      ans += 2*left;
      left = 0;
    } else {
      ans += 2*right;
      right = 0;
    }
  }
  return ans;
}

void solve() {
  cin >> n;
  cin >> s;

  int ans = min(res('0'), res('1'));
  cout << ans << endl;
}

int main() {_;

  int ttt; cin >> ttt;
  while (ttt--) solve();

  return 0;
}

Submission Info

Submission Time
Task D - Pop and Insert
User luishgh
Language C++ 20 (gcc 12.2)
Score 400
Code Size 1372 Byte
Status AC
Exec Time 11 ms
Memory 3796 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 3404 KiB
01_small_00.txt AC 11 ms 3612 KiB
02_random_00.txt AC 3 ms 3532 KiB
02_random_01.txt AC 2 ms 3556 KiB
02_random_02.txt AC 2 ms 3512 KiB
02_random_03.txt AC 2 ms 3744 KiB
02_random_04.txt AC 2 ms 3740 KiB
02_random_05.txt AC 4 ms 3564 KiB
02_random_06.txt AC 3 ms 3644 KiB
03_handmade_00.txt AC 3 ms 3640 KiB
03_handmade_01.txt AC 3 ms 3636 KiB
03_handmade_02.txt AC 3 ms 3796 KiB
03_handmade_03.txt AC 2 ms 3720 KiB
03_handmade_04.txt AC 3 ms 3784 KiB