#include <bits/stdc++.h> #ifndef ONLINE_JUDGE # pragma GCC diagnostic warning "-Wall" # pragma GCC diagnostic warning "-Wextra" # pragma GCC diagnostic warning "-Wformat=2" # pragma GCC diagnostic warning "-Wnull-dereference" # pragma GCC diagnostic warning "-Wduplicated-branches" # pragma GCC diagnostic warning "-Wduplicated-cond" # pragma GCC diagnostic warning "-Wfloat-equal" # pragma GCC diagnostic warning "-Wshadow" # pragma GCC diagnostic warning "-Wconversion" # pragma GCC diagnostic warning "-Wlogical-op" # pragma GCC diagnostic warning "-Wvector-operation-performance" # pragma GCC diagnostic warning "-Wdisabled-optimization" # pragma GCC diagnostic warning "-Wunsafe-loop-optimizations" # pragma GCC diagnostic warning "-Wdouble-promotion" #endif #pragma GCC target "arch=ivybridge", "tune=ivybridge" #if defined(ONLINE_JUDGE) || !defined(__OPTIMIZE__) # pragma GCC optimize "Ofast", "inline", "unroll-loops", "ipa-pta", "no-rtti", \ "no-exceptions", "nothrow-opt", "strict-enums", "stdarg-opt", "tracer" # pragma GCC target "inline-all-stringops" #endif #define rangeof(c) (c).begin(), (c).end() using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using ld = long double; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int qc; cin >> qc; for(int query = 0; query < qc; query++) { #ifdef DEBUG clog << "teścior " << query << endl; #endif int n; cin >> n; string s; cin >> s; vector<int> twoc_of_size(n + 1, 0), ones; int twoc = 0; int block = 0; bool is_all_zero = false; for(int i = 0; i < n; i++) { if(s[i] == '0') block++; if((i + 1 >= n || s[i + 1] == '1') && block > 0) { int infected_sidec = 2 - (i - block < 0) - (i == n - 1); if(infected_sidec == 2) { twoc_of_size[block]++; twoc++; } else if(infected_sidec == 1) { ones.push_back(block); } else if(infected_sidec == 0) { is_all_zero = true; } else { assert(false); } block = 0; } } if(is_all_zero) { cout << "0\n"; continue; } sort(rangeof(ones), greater<int>()); // we know ones.size() is two at most vector<int> twos; twos.reserve(twoc); for(int size = n; size >= 0; size--) { while(twoc_of_size[size] > 0) { twoc_of_size[size]--; twos.push_back(size); } } int result = 0; auto calc_twos_val = [&](int time) { #ifdef DEBUG clog << "czesc" << endl; #endif int result = 0; for(int size: twos) { int actual = size - 2 * time; if(actual <= 0) { break; } if(actual <= 2) { actual = 1; result++; time++; } else { actual--; result += actual; time += 2; } #ifdef DEBUG clog << size << " " << actual << endl; #endif } return result; }; result = max(calc_twos_val(0), (ones.size() < 1 ? 0 : max(ones[0] + calc_twos_val(1), (ones.size() < 2 ? 0 : max(ones[1] + calc_twos_val(1), ones[0] + ones[1] - 1 + calc_twos_val(2)))))); cout << n - result << "\n"; } } /* 2 1 x+1 x napierw lewe -> x+x-2 najpier prawe -> x-1+x 2 2 1 x+1 x+1 x najperw lewe -> x+x-2+x-4 nparwer prawe -> x-3+x-1+x 2 2 11 8 l,p,l,p -> 11+8 - 3 - 2 - 1 - 0 l,l,p,p -> 11+8 - 3 - 2 - 1 - 0 1 2 2 3 11 8 l,m,m,p,p -> 3 11 8 3 9 6 after l 3 8 4 after m 3 8 2 after m 3 8 1 after p 3 8 1 after p */
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | #include <bits/stdc++.h> #ifndef ONLINE_JUDGE # pragma GCC diagnostic warning "-Wall" # pragma GCC diagnostic warning "-Wextra" # pragma GCC diagnostic warning "-Wformat=2" # pragma GCC diagnostic warning "-Wnull-dereference" # pragma GCC diagnostic warning "-Wduplicated-branches" # pragma GCC diagnostic warning "-Wduplicated-cond" # pragma GCC diagnostic warning "-Wfloat-equal" # pragma GCC diagnostic warning "-Wshadow" # pragma GCC diagnostic warning "-Wconversion" # pragma GCC diagnostic warning "-Wlogical-op" # pragma GCC diagnostic warning "-Wvector-operation-performance" # pragma GCC diagnostic warning "-Wdisabled-optimization" # pragma GCC diagnostic warning "-Wunsafe-loop-optimizations" # pragma GCC diagnostic warning "-Wdouble-promotion" #endif #pragma GCC target "arch=ivybridge", "tune=ivybridge" #if defined(ONLINE_JUDGE) || !defined(__OPTIMIZE__) # pragma GCC optimize "Ofast", "inline", "unroll-loops", "ipa-pta", "no-rtti", \ "no-exceptions", "nothrow-opt", "strict-enums", "stdarg-opt", "tracer" # pragma GCC target "inline-all-stringops" #endif #define rangeof(c) (c).begin(), (c).end() using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; using ld = long double; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int qc; cin >> qc; for(int query = 0; query < qc; query++) { #ifdef DEBUG clog << "teścior " << query << endl; #endif int n; cin >> n; string s; cin >> s; vector<int> twoc_of_size(n + 1, 0), ones; int twoc = 0; int block = 0; bool is_all_zero = false; for(int i = 0; i < n; i++) { if(s[i] == '0') block++; if((i + 1 >= n || s[i + 1] == '1') && block > 0) { int infected_sidec = 2 - (i - block < 0) - (i == n - 1); if(infected_sidec == 2) { twoc_of_size[block]++; twoc++; } else if(infected_sidec == 1) { ones.push_back(block); } else if(infected_sidec == 0) { is_all_zero = true; } else { assert(false); } block = 0; } } if(is_all_zero) { cout << "0\n"; continue; } sort(rangeof(ones), greater<int>()); // we know ones.size() is two at most vector<int> twos; twos.reserve(twoc); for(int size = n; size >= 0; size--) { while(twoc_of_size[size] > 0) { twoc_of_size[size]--; twos.push_back(size); } } int result = 0; auto calc_twos_val = [&](int time) { #ifdef DEBUG clog << "czesc" << endl; #endif int result = 0; for(int size: twos) { int actual = size - 2 * time; if(actual <= 0) { break; } if(actual <= 2) { actual = 1; result++; time++; } else { actual--; result += actual; time += 2; } #ifdef DEBUG clog << size << " " << actual << endl; #endif } return result; }; result = max(calc_twos_val(0), (ones.size() < 1 ? 0 : max(ones[0] + calc_twos_val(1), (ones.size() < 2 ? 0 : max(ones[1] + calc_twos_val(1), ones[0] + ones[1] - 1 + calc_twos_val(2)))))); cout << n - result << "\n"; } } /* 2 1 x+1 x napierw lewe -> x+x-2 najpier prawe -> x-1+x 2 2 1 x+1 x+1 x najperw lewe -> x+x-2+x-4 nparwer prawe -> x-3+x-1+x 2 2 11 8 l,p,l,p -> 11+8 - 3 - 2 - 1 - 0 l,l,p,p -> 11+8 - 3 - 2 - 1 - 0 1 2 2 3 11 8 l,m,m,p,p -> 3 11 8 3 9 6 after l 3 8 4 after m 3 8 2 after m 3 8 1 after p 3 8 1 after p */ |