提出 #69853787
ソースコード 拡げる
// 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;
}
提出情報
| 提出日時 | |
|---|---|
| 問題 | D - Pop and Insert |
| ユーザ | luishgh |
| 言語 | C++ 20 (gcc 12.2) |
| 得点 | 400 |
| コード長 | 1372 Byte |
| 結果 | AC |
| 実行時間 | 11 ms |
| メモリ | 3796 KiB |
ジャッジ結果
| セット名 | Sample | All | ||||
|---|---|---|---|---|---|---|
| 得点 / 配点 | 0 / 0 | 400 / 400 | ||||
| 結果 |
|
|
| セット名 | テストケース |
|---|---|
| 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 |
| ケース名 | 結果 | 実行時間 | メモリ |
|---|---|---|---|
| 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 |