Submission #70199225


Source Code Expand

#include <stdio.h>

int N;
char S[512345];

int main(void) {
	int T, tc;
	if (scanf("%d", &T) != 1) return 1;
	for (tc = 0; tc < T; tc++) {
		int zero_all = 0, zero_cur = 0, zero_max = 0;
		int one_all = 0, one_cur = 0, one_max = 0;
		int i;
		int c1, c2;
		if (scanf("%d", &N) != 1) return 1;
		if (scanf("%512343s", S + 1) != 1) return 1;
		for (i = 1; i <= N; i++) {
			if (S[i] == '1') {
				one_all++;
				if (++one_cur > one_max) one_max = one_cur;
				zero_cur = 0;
			} else {
				zero_all++;
				if (++zero_cur > zero_max) zero_max = zero_cur;
				one_cur = 0;
			}
		}
		c1 = (zero_all - zero_max) * 2 + one_all;
		c2 = (one_all - one_max) * 2 + zero_all;
		printf("%d\n", c1 <= c2 ? c1 : c2);
	}
	return 0;
}

/*

多分一番でかいブロックを活用するのがいい
残すブロック以外で
残すブロックと同じ文字 → 2回反転
残すブロックと違う文字 → 1回反転
いずれも、操作後残すブロックに突っ込めばいい

*/

Submission Info

Submission Time
Task D - Pop and Insert
User mikecat
Language C (gcc 12.2.0)
Score 400
Code Size 1025 Byte
Status AC
Exec Time 9 ms
Memory 2228 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 0 ms 1624 KiB
01_small_00.txt AC 9 ms 1716 KiB
02_random_00.txt AC 2 ms 1616 KiB
02_random_01.txt AC 2 ms 1644 KiB
02_random_02.txt AC 2 ms 1784 KiB
02_random_03.txt AC 2 ms 2172 KiB
02_random_04.txt AC 2 ms 2192 KiB
02_random_05.txt AC 2 ms 2052 KiB
02_random_06.txt AC 2 ms 2224 KiB
03_handmade_00.txt AC 2 ms 2228 KiB
03_handmade_01.txt AC 2 ms 2204 KiB
03_handmade_02.txt AC 2 ms 2112 KiB
03_handmade_03.txt AC 2 ms 2056 KiB
03_handmade_04.txt AC 2 ms 2080 KiB