Submission #66853497
Source Code Expand
Copy
#include <stdio.h>int T, tc;int N;char S[212345];int memo[212345][3];int memo_tc[212345][3];int calc(int pos, int status) {int ans, candidate;if (pos >= N) return 0;if (memo_tc[pos][status] == tc) return memo[pos][status];/* 変えない */ans = calc(pos + 1, status) + (S[pos] != "010"[status]);/* 次に進める */if (status < 2) {candidate = calc(pos + 1, status + 1) + (S[pos] != "10"[status]);if (candidate < ans) ans = candidate;}
#include <stdio.h>
int T, tc;
int N;
char S[212345];
int memo[212345][3];
int memo_tc[212345][3];
int calc(int pos, int status) {
	int ans, candidate;
	if (pos >= N) return 0;
	if (memo_tc[pos][status] == tc) return memo[pos][status];
	/* 変えない */
	ans = calc(pos + 1, status) + (S[pos] != "010"[status]);
	/* 次に進める */
	if (status < 2) {
		candidate = calc(pos + 1, status + 1) + (S[pos] != "10"[status]);
		if (candidate < ans) ans = candidate;
	}
	memo[pos][status] = ans;
	memo_tc[pos][status] = tc;
	return ans;
}
int main(void) {
	if (scanf("%d", &T) != 1) return 1;
	for (tc = 1; tc <= T; tc++) {
#if 0
		int i;
		int cnt = 0, combo = 0, max_combo = 0;
		int first = -1, last = -1;
		int cnt2 = 0, candidate;
#endif
		if (scanf("%d", &N) != 1) return 1;
		if (scanf("%212344s", S) != 1) return 1;
#if 0
		for (i = 0; i < N; i++) {
			if (S[i] == '1') {
				cnt++;
				combo++;
				if (combo > max_combo) max_combo = combo;
				if (first == -1) first = i;
				last = i;
			} else {
				combo = 0;
			}
		}
		candidate = cnt - max_combo;
		for (i = first; i <= last; i++) {
			if (S[i] != '1') cnt2++;
		}
		printf("%d\n", candidate <= cnt2 ? candidate : cnt2);
#endif
		printf("%d\n", calc(0, 0));
	}
	return 0;
}
/*
連続する同じ数字を1つにして 010101… みたいな形にしたとき、「1」を1個以下にできれば勝ち
1回の操作で、「1」を1個減らせる
↑大嘘 1回の操作で変えられるのは範囲ではなく、1要素のみ
とりうる戦略は
・最大の「1」のかたまりだけを残し、他の「1」を全て潰す
・「1」の間に挟まる「0」を全て潰す
↑嘘 ハイブリッドの可能性もある
1
13
1000001110111
「0:0にする」「1:1にする」「2:0にする」の状態を持つ
それぞれの数字について、状態を「変えない」「次に進める」を選択するメモ化探索
*/
			Submission Info
| Submission Time | |
|---|---|
| Task | D - Flip to Gather | 
| User | mikecat | 
| Language | C (gcc 12.2.0) | 
| Score | 400 | 
| Code Size | 2004 Byte | 
| Status | AC | 
| Exec Time | 13 ms | 
| Memory | 19100 KiB | 
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 400 / 400 | ||||
| Status | 
 | 
 | 
| Set Name | Test Cases | 
|---|---|
| Sample | 00_sample_00.txt, 00_sample_01.txt | 
| All | 00_sample_00.txt, 00_sample_01.txt, 01_test_00.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 01_test_09.txt, 01_test_10.txt, 01_test_11.txt, 01_test_12.txt, 01_test_13.txt, 01_test_14.txt, 01_test_15.txt, 01_test_16.txt, 01_test_17.txt, 01_test_18.txt, 01_test_19.txt, 01_test_20.txt, 01_test_21.txt, 01_test_22.txt, 01_test_23.txt, 01_test_24.txt, 01_test_25.txt, 01_test_26.txt, 01_test_27.txt | 
| Case Name | Status | Exec Time | Memory | 
|---|---|---|---|
| 00_sample_00.txt | AC | 1 ms | 1624 KiB | 
| 00_sample_01.txt | AC | 1 ms | 1612 KiB | 
| 01_test_00.txt | AC | 7 ms | 1712 KiB | 
| 01_test_01.txt | AC | 6 ms | 1720 KiB | 
| 01_test_02.txt | AC | 6 ms | 1632 KiB | 
| 01_test_03.txt | AC | 6 ms | 1564 KiB | 
| 01_test_04.txt | AC | 6 ms | 1708 KiB | 
| 01_test_05.txt | AC | 6 ms | 1700 KiB | 
| 01_test_06.txt | AC | 6 ms | 1700 KiB | 
| 01_test_07.txt | AC | 6 ms | 1716 KiB | 
| 01_test_08.txt | AC | 6 ms | 1724 KiB | 
| 01_test_09.txt | AC | 5 ms | 1572 KiB | 
| 01_test_10.txt | AC | 6 ms | 1684 KiB | 
| 01_test_11.txt | AC | 6 ms | 1636 KiB | 
| 01_test_12.txt | AC | 6 ms | 1700 KiB | 
| 01_test_13.txt | AC | 6 ms | 1628 KiB | 
| 01_test_14.txt | AC | 6 ms | 1704 KiB | 
| 01_test_15.txt | AC | 6 ms | 1780 KiB | 
| 01_test_16.txt | AC | 6 ms | 2568 KiB | 
| 01_test_17.txt | AC | 6 ms | 2416 KiB | 
| 01_test_18.txt | AC | 6 ms | 2568 KiB | 
| 01_test_19.txt | AC | 11 ms | 13348 KiB | 
| 01_test_20.txt | AC | 11 ms | 13292 KiB | 
| 01_test_21.txt | AC | 12 ms | 13624 KiB | 
| 01_test_22.txt | AC | 13 ms | 19100 KiB | 
| 01_test_23.txt | AC | 13 ms | 19060 KiB | 
| 01_test_24.txt | AC | 13 ms | 19080 KiB | 
| 01_test_25.txt | AC | 13 ms | 19004 KiB | 
| 01_test_26.txt | AC | 13 ms | 18920 KiB | 
| 01_test_27.txt | AC | 13 ms | 19100 KiB |