题目链接
思路
$$ 反转问题, 第一个反转与否决定全部 $$
时间复杂度
$$ O(N) $$
代码
#include <cstdio>
#include <algorithm>
using namespace std;
int a[25];
int b[25];
void f(int x) {
b[x - 1] ^= 1;
b[x] ^= 1;
b[x + 1] ^= 1;
}
int gao(int x) {
int cnt = 0;
for (int i = 1; i <= 20; i++) {
b[i] = a[i];
}
if (x == 1) {
f(1);
cnt++;
}
for (int i = 2; i <= 20; i++) {
if (b[i - 1] == 1) {
f(i);
cnt++;
}
}
if (b[20] == 1) {
return 20;
} else {
return cnt;
}
}
int main() {
for (int i = 1; i <= 20; i++) {
scanf("%d", &a[i]);// don't forget &
}
printf("%d", min(gao(0), gao(1)));
return 0;
}