1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<bits/stdc++.h>
using namespace std;

bool b[100010];

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int e;
cin >> e;
while (e--) {
int n;
cin >> n;
char c;
bool infected = false;
for(int i = 0; i < n; i++) {
    cin >> c;
    b[i] = (c == '1');
    infected |= b[i];
}
if (!infected) {
    cout << "0\n";
    continue;
}
vector <int> v = {};
int krance[2] = {0, 0};
int pocz = 0;
while(!b[pocz]) {
    pocz++;
}
krance[0] = pocz;
int kon  = n-1;
while(!b[kon]) {
    kon--;
}
krance[1] = n-1-kon;
if(krance[1] > krance[0]) {
    swap(krance[0], krance[1]);
}
int conszeros = 0;
for(int i = pocz; i <= kon; i++) {
    if(b[i] == true) {
        if(conszeros > 0) {
            v.push_back(conszeros);
        }
        conszeros = 0;
    }
    else {
        conszeros++;
    }
}
sort(v.begin(), v.end(), std::greater<int>());
int vsum = 0, v2sum = 0, v3sum = 0;
vector <int> v2 = {}, v3 = {};
for(int i = 0; i < v.size(); i++) {
    v2.push_back((max(v[i] - 3 - 2 * i, 0)) + (v[i] - 2 - 2 * i == 1));
    v2sum += v2[i];
    v3.push_back((max(v[i] - 5 - 2 * i, 0)) + (v[i] - 4 - 2 * i == 1));
    v3sum += v3[i];
}
for(int i = 0; i < v.size(); i++) {
    v[i] = max(v[i] - 1 - 2 * i, 0) + (v[i] - 2 * i == 1);
    vsum += v[i];
}
int saved = max(vsum, max(krance[0] + v2sum, krance[0] + krance[1] - 1 + v3sum));
//cout << vsum << " " << krance[0] + v2sum << " " << krance[0] + krance[1] - 1 + v3sum << "\n";
cout << n - saved << "\n";
}
}