#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int lef, righ;
int t, n, last_vir;
char vir;
vector<int> ranges;
int main() {
    cin >> t;
    while (t--) {
        ranges.clear();
        lef = -1;
        righ = -1;
        cin >> n;
        last_vir = -1;
        for (int i = 1; i <= n; i++) {
            cin >> vir;
            if (vir == '1') {
                if (lef == -1) {
                    lef = i - 1;
                } else if (last_vir != i-1) {
                    ranges.push_back(i - last_vir - 1);
                }
                last_vir = i;
            }
        }
        if (last_vir == -1) {
            cout << "0" << endl;
            continue;
        }
        righ = n - last_vir;
        if (righ > lef) {
            swap(righ, lef);
        }
        sort(ranges.begin(), ranges.end(), greater<int>());
        int count = 0;
        int pos = 0;
        int time = 0;
        while (1) {
            if ((lef - time <= 0) && (pos >= ranges.size() || ranges[pos] - 2*time <= 0)) {
                break;
            }
            if ((pos < ranges.size()) && (ranges[pos] - 2*time >= 2*(lef - time))) {
                if (ranges[pos] - 2*time == 1) {
                    count += 1;
                    time++;
                } else {
                    count += ranges[pos] - 2*time - 1;
                    time += 2;
                }
                pos++;
            } else {
                count += lef - time;
                time++;
                lef = righ;
                righ = -1;
            }
        }
        cout << n - count << endl;
    }
    return 0;
}
        | 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 | #include <bits/stdc++.h> #include <iostream> using namespace std; int lef, righ; int t, n, last_vir; char vir; vector<int> ranges; int main() { cin >> t; while (t--) { ranges.clear(); lef = -1; righ = -1; cin >> n; last_vir = -1; for (int i = 1; i <= n; i++) { cin >> vir; if (vir == '1') { if (lef == -1) { lef = i - 1; } else if (last_vir != i-1) { ranges.push_back(i - last_vir - 1); } last_vir = i; } } if (last_vir == -1) { cout << "0" << endl; continue; } righ = n - last_vir; if (righ > lef) { swap(righ, lef); } sort(ranges.begin(), ranges.end(), greater<int>()); int count = 0; int pos = 0; int time = 0; while (1) { if ((lef - time <= 0) && (pos >= ranges.size() || ranges[pos] - 2*time <= 0)) { break; } if ((pos < ranges.size()) && (ranges[pos] - 2*time >= 2*(lef - time))) { if (ranges[pos] - 2*time == 1) { count += 1; time++; } else { count += ranges[pos] - 2*time - 1; time += 2; } pos++; } else { count += lef - time; time++; lef = righ; righ = -1; } } cout << n - count << endl; } return 0; } | 
 
            
         English
                    English