#include<iostream> using namespace std; int number[100100]; void test() { int n; cin >> n; for (int i = 0; i < n; i++) number[i] = 0; string a; cin >> a; int pocz = 0, kon = n; while(pocz < n && a[pocz] == '0') {pocz++;} if (pocz == n) {cout << "0\n"; return;}// nikt nie zarażony while(a[kon - 1] == '0') {kon--;} int lewo = pocz, prawo = n - kon; int ctr = 0; for (int i = pocz; i < kon; i++) { if (a[i] == '1') { number[ctr]++; ctr = -1; } ctr++; } int time = 0; int i; int res = 0; for (i = n; i >= 3 + 2 * time; i--) { while (number[i] && i >= 3 + 2 * time) { number[i]--; res += i - 2 * time - 1; time += 2; } } if (lewo - time > 0) { res+= lewo - time; time++; } if (prawo - time > 0) { res+= prawo - time; time++; } if (2 * time + 2 < n && (number[2 * time + 1] || number[2 * time + 2])) res++; cout << n - res << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--) {test();} 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 | #include<iostream> using namespace std; int number[100100]; void test() { int n; cin >> n; for (int i = 0; i < n; i++) number[i] = 0; string a; cin >> a; int pocz = 0, kon = n; while(pocz < n && a[pocz] == '0') {pocz++;} if (pocz == n) {cout << "0\n"; return;}// nikt nie zarażony while(a[kon - 1] == '0') {kon--;} int lewo = pocz, prawo = n - kon; int ctr = 0; for (int i = pocz; i < kon; i++) { if (a[i] == '1') { number[ctr]++; ctr = -1; } ctr++; } int time = 0; int i; int res = 0; for (i = n; i >= 3 + 2 * time; i--) { while (number[i] && i >= 3 + 2 * time) { number[i]--; res += i - 2 * time - 1; time += 2; } } if (lewo - time > 0) { res+= lewo - time; time++; } if (prawo - time > 0) { res+= prawo - time; time++; } if (2 * time + 2 < n && (number[2 * time + 1] || number[2 * time + 2])) res++; cout << n - res << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--) {test();} return 0; } |