#define _USE_MATH_DEFINES //#pragma GCC optimize("O3") // #pragma GCC target("sse4") #include <bits/stdc++.h> using namespace std; #define LSB(i) ((i) & -(i)) // zeroes all the bits except the least significant one #define OPEN(a) freopen(a, "r", stdin) #define F first #define S second #define PB push_back #define BOOST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define ALL(i) begin((i)), end((i)) #define SZ(i) ((int)i.size()) #define SORT(a) sort(ALL(a)) using LL = long long; using LD = long double;//__float128; using PII = pair<int, int>; using PLL = pair<LL, LL>; using PLD = pair<LD, LD>; using VI = vector<int>; using VLL = vector<LL>; using VLD = vector<LD>; using VPII = vector<PII>; using VPLL = vector<PLL>; using VPLD = vector<PLD>; using VVI = vector<VI>; #define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME #define __FOR3(i, a, n, inc) for(int i = (a); (inc) > 0 ? i < (n) : i >= (n); i += (inc)) #define __FOR2(i, a, n) __FOR3(i, a, n, 1) #define __FOR1(i, n) __FOR2(i, 0, n) #define FOR(...) GET_MACRO(__VA_ARGS__, __FOR3, __FOR2, __FOR1)(__VA_ARGS__) #define REV(a,b) for(int a= b; a >= 0; --a) #define FRU(a,b) for(const auto& a: b) #define FRUM(a,b) for(auto& a : b) const int inf = 1e9 + 7; const int MOD = inf;//998244353; const LL INF = 1e18 + 7; const long double PI = acos(-1); const LD EPS = 1e-9; namespace input { template< class T> istream& operator>>(istream& st, vector<T> & container) { for (auto& u : container) st >> u; return st; } template< class T, size_t N> istream& operator>>(istream& st, array<T, N> & container) { for (auto& u : container) st >> u; return st; } template <class T, class U> istream& operator>>(istream& st, pair<T, U> & p) { st >> p.first >> p.second; return st; } void re() {} template<typename T, typename... TArgs> void re(T& x, TArgs&... rest) { cin >> x; re(rest...); } } using namespace input; namespace output { template< class T> ostream& operator<<(ostream& st, const vector<T> & container) { for (auto& u : container) st << u << ' '; return st; } template< class T, size_t N> ostream& operator<<(ostream& st, const array<T, N> & container) { for (auto& u : container) st << u << ' '; return st; } template <class T, class U> ostream& operator<<(ostream& st, pair<T, U> p) { st << p.first << ' ' << p.second; return st; } void pr() {} template <typename T> void pr(const T& x) { cout << x; } template <typename T, typename... TArgs> void pr(const T& x, const TArgs&... rest) { cout << x << ' '; pr(rest...); } template <typename... TArgs> void prln(const TArgs&... args) { pr(args...); cout << '\n'; } } using namespace output; namespace pairs { template<class T, class U, class V> pair<T, U> operator* (pair<T, U>p, V val) { return{ p.first * val, p.second * val }; } template<class T, class U, class V> pair<T, U> operator/ (pair<T, U>p, V val) { return{ p.first / val, p.second / val }; } template<class T, class U> pair<T, U> operator- (pair<T, U> a, pair<T, U> b) { return{ a.first - b.first, a.second - b.second }; } template<class T, class U> pair<T, U> operator+ (pair<T, U> a, pair<T, U> b) { return{ a.first + b.first, a.second + b.second }; } } using namespace pairs; namespace triples { #define TT1T2T3 template<class T1, class T2, class T3> #define TT1T2T3T4 template<class T1, class T2, class T3, class T4> #define TRT1T2T3 triple<T1, T2, T3> TT1T2T3 struct triple { T1 x; T2 y; T3 z; triple() : x(T1()), y(T2()), z(T3()) {}; triple(T1 _x, T2 _y, T3 _z) :x(_x), y(_y), z(_z) {} }; TT1T2T3 bool operator<(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.x != t2.x)return t1.x < t2.x; if (t1.y != t2.y) return t1.y < t2.y; else return t1.z < t2.z; } TT1T2T3 bool operator>(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.x != t2.x)return t1.x > t2.x; if (t1.y != t2.y) return t1.y > t2.y; else return t1.z > t2.z; } TT1T2T3 bool operator==(const TRT1T2T3&t1, const TRT1T2T3&t2) { return (t1.x == t2.x && t1.y == t2.y && t1.z == t2.z); } TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.x >> t.y >> t.y; } TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t) { return os << t.x << " " << t.y << " " << t.z; } TT1T2T3 TRT1T2T3 operator+(TRT1T2T3 a, TRT1T2T3 b) { return { a.x + b.x, a.y + b.y, a.z + b.z }; } TT1T2T3 TRT1T2T3 operator-(TRT1T2T3 a, TRT1T2T3 b) { return { a.x - b.x, a.y - b.y, a.z - b.z }; } TT1T2T3T4 TRT1T2T3 operator*(TRT1T2T3 a, T4 val) { return { a.x * val, a.y * val, a.z * val }; } TT1T2T3T4 TRT1T2T3 operator/(TRT1T2T3 a, T4 val) { return { a.x / val, a.y / val, a.z / val }; } #undef TT1T2T3T4 #undef TRT1T2T3 #undef TT1T2T3 using TRII = triple<int, int, int>; using TRLL = triple<LL, LL, LL>; using TRLD = triple<LD, LD, LD>; using VTRII = vector<TRII>; using VTRLL = vector<TRLL>; using VTRLD = vector<TRLD>; } using namespace triples; namespace geo { template<class T> T dotProduct(pair<T, T> a, pair<T, T> b) { return a.first*b.first + a.second* b.second; } template<class T> T crossProduct(pair<T, T>a, pair<T, T> b) { return a.first * b.second - a.second * b.first; } template<class T> T lengthPow(pair<T, T> a) { return a.first*1ll*a.first + a.second*1ll*a.second; } template<class T> LD length(pair<T, T> a) { return sqrt(lengthPow(a)); } template<class T> T dotProduct(triple<T, T, T> a, triple<T, T, T> b) { return a.x*b.x + a.y* b.y + a.z*b.z; } template<class T> T crossProduct(triple<T, T, T> a, triple<T, T, T> b) { return { a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x }; } template<class T> T lengthPow(triple<T, T, T> a) { return a.x*1ll*a.x + a.y*1ll*a.y + a.z*1ll*a.z; } template<class T> LD length(triple<T, T, T> a) { return sqrt(lengthPow(a)); } } using namespace geo; template <class T> T invGeneral(T a, T b) { // 0 < a < b, gcd(a,b) = 1 a %= b; if (a == 0) return b == 1 ? 0 : -1; T x = invGeneral(b, a); return x == -1 ? -1 : ((1 - (LL)b * x) / a + b) % b; } template<class T> struct modular { T val; explicit operator T() const { return val; } modular() { val = 0; } modular(const LL& v) { val = (-MOD <= v && v <= MOD) ? v : v % MOD; if (val < 0) val += MOD; } friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; } friend void pr(const modular& a) { pr(a.val); } friend void re(modular& a) { LL x; re(x); a = modular(x); } friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; } friend bool operator!=(const modular& a, const modular& b) { return !(a == b); } friend bool operator<(const modular& a, const modular& b) { return a.val < b.val; } modular operator-() const { return modular(-val); } modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; } modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; } modular& operator*=(const modular& m) { val = (LL)val*m.val%MOD; return *this; } friend modular pow(modular a, LL p) { modular ans = 1; for (; p; p /= 2, a *= a) if (p & 1) ans *= a; return ans; } friend modular inv(const modular& a) { auto i = invGeneral(a.val, MOD); assert(i != -1); return i; } // equivalent to return exp(b,MOD-2) if MOD is prime modular& operator/=(const modular& m) { return (*this) *= inv(m); } friend modular operator+(modular a, const modular& b) { return a += b; } friend modular operator-(modular a, const modular& b) { return a -= b; } friend modular operator*(modular a, const modular& b) { return a *= b; } friend modular operator/(modular a, const modular& b) { return a /= b; } }; using MI = modular<int>; using PMI = pair<MI, MI>; using VMI = vector<MI>; using VPMI = vector<PMI>; namespace debug { template < typename _T > inline void _debug(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _debug(const char *s, _T x, args... a) { while (*s != ',') cerr << *s++; cerr << " = " << x << ','; _debug(s + 1, a...); } #if 0 && defined(LOCAL) #define debug(...) _debug(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) 1999 #define cerr if(0) cout #endif } using namespace debug; template <class T> bool setMax(T& v, T newV) { if (v < newV) { v = newV; return true; } return false; } template <class T> bool setMin(T& v, T newV) { if (v > newV) { v = newV; return true; } return false; } int calc(const vector<int>& lengths, const vector<int>& ends, int pos) { if(pos < 0) return 0; if(pos > lengths.size()) return 0; int res = 0; int cnt = 0; for(int i = 0; i < pos; ++i){ res += lengths[i] - (2 * cnt + 1); cnt += 2; } cerr << "co sie dzieje " << cnt << ' ' << res << '\n'; for(auto u : ends) { if(u > cnt) { res += u - cnt; ++cnt; } } cerr << "konce done " << cnt << ' ' << res << '\n'; while(cnt < lengths.size() and lengths[pos] > 2 * cnt) { cerr << "OKKK\n"; cerr << lengths[pos] << ' ' << cnt << '\n'; res++; cnt++; } return res; } void solve() { int n; cin >> n; string s; cin >> s; cerr << "tescior\n"; int beg = -1; vector<int> lengths; int last = -1; for(int i = 0; i < n; ++i) { if(s[i] == '0') continue; if(beg == -1) beg = i; if(last != -1 and last < i - 1) lengths.push_back(i - last - 1); last = i; } vector<int> ends = {beg, n - last - 1}; sort(ends.begin(), ends.end(), greater<int>()); sort(lengths.begin(), lengths.end(), greater<int>()); vector<int> dp = {0, ends[0], ends[0] + max(0, ends[1] - 1)}; int pos = 0; for(auto u : lengths) { vector<int> newDp(3); newDp[0] = dp[0] + max({0, u - (2 * 2 * pos + 1), min(1, u - 2 * 2 * pos)}); newDp[1] = dp[1] + max({0, u - (2 * (2 * pos + 1) + 1), min(1, u - 2 * (2 * pos + 1))}); newDp[2] = dp[2] + max({0, u - (2 * (2 * pos + 2) + 1), min(1, u - 2 * (2 * pos + 2))}); newDp[1] = max(newDp[1], dp[0] + max(0, ends[0] - 2 * pos)); newDp[2] = max(newDp[2], dp[1] + max(0, ends[1] - (2 * pos + 1))); newDp[2] = max(newDp[2], dp[0] + max(0, ends[0] - 2 * pos) + max(0, ends[1] - (2 * pos + 1))); swap(newDp, dp); ++pos; } cout << n - max({dp[0], dp[1], dp[2]}) << '\n'; } int main() { // #ifndef LOCAL // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif BOOST; cout << fixed << setprecision(13); int t = 1; cerr << "testy!!!!\n"; cin >> t; for(int i = 1; i <= t; ++i) // string s; // while(cin >> s) { // cout << "Case #" << i << ": "; solve(); } 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 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | #define _USE_MATH_DEFINES //#pragma GCC optimize("O3") // #pragma GCC target("sse4") #include <bits/stdc++.h> using namespace std; #define LSB(i) ((i) & -(i)) // zeroes all the bits except the least significant one #define OPEN(a) freopen(a, "r", stdin) #define F first #define S second #define PB push_back #define BOOST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define ALL(i) begin((i)), end((i)) #define SZ(i) ((int)i.size()) #define SORT(a) sort(ALL(a)) using LL = long long; using LD = long double;//__float128; using PII = pair<int, int>; using PLL = pair<LL, LL>; using PLD = pair<LD, LD>; using VI = vector<int>; using VLL = vector<LL>; using VLD = vector<LD>; using VPII = vector<PII>; using VPLL = vector<PLL>; using VPLD = vector<PLD>; using VVI = vector<VI>; #define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME #define __FOR3(i, a, n, inc) for(int i = (a); (inc) > 0 ? i < (n) : i >= (n); i += (inc)) #define __FOR2(i, a, n) __FOR3(i, a, n, 1) #define __FOR1(i, n) __FOR2(i, 0, n) #define FOR(...) GET_MACRO(__VA_ARGS__, __FOR3, __FOR2, __FOR1)(__VA_ARGS__) #define REV(a,b) for(int a= b; a >= 0; --a) #define FRU(a,b) for(const auto& a: b) #define FRUM(a,b) for(auto& a : b) const int inf = 1e9 + 7; const int MOD = inf;//998244353; const LL INF = 1e18 + 7; const long double PI = acos(-1); const LD EPS = 1e-9; namespace input { template< class T> istream& operator>>(istream& st, vector<T> & container) { for (auto& u : container) st >> u; return st; } template< class T, size_t N> istream& operator>>(istream& st, array<T, N> & container) { for (auto& u : container) st >> u; return st; } template <class T, class U> istream& operator>>(istream& st, pair<T, U> & p) { st >> p.first >> p.second; return st; } void re() {} template<typename T, typename... TArgs> void re(T& x, TArgs&... rest) { cin >> x; re(rest...); } } using namespace input; namespace output { template< class T> ostream& operator<<(ostream& st, const vector<T> & container) { for (auto& u : container) st << u << ' '; return st; } template< class T, size_t N> ostream& operator<<(ostream& st, const array<T, N> & container) { for (auto& u : container) st << u << ' '; return st; } template <class T, class U> ostream& operator<<(ostream& st, pair<T, U> p) { st << p.first << ' ' << p.second; return st; } void pr() {} template <typename T> void pr(const T& x) { cout << x; } template <typename T, typename... TArgs> void pr(const T& x, const TArgs&... rest) { cout << x << ' '; pr(rest...); } template <typename... TArgs> void prln(const TArgs&... args) { pr(args...); cout << '\n'; } } using namespace output; namespace pairs { template<class T, class U, class V> pair<T, U> operator* (pair<T, U>p, V val) { return{ p.first * val, p.second * val }; } template<class T, class U, class V> pair<T, U> operator/ (pair<T, U>p, V val) { return{ p.first / val, p.second / val }; } template<class T, class U> pair<T, U> operator- (pair<T, U> a, pair<T, U> b) { return{ a.first - b.first, a.second - b.second }; } template<class T, class U> pair<T, U> operator+ (pair<T, U> a, pair<T, U> b) { return{ a.first + b.first, a.second + b.second }; } } using namespace pairs; namespace triples { #define TT1T2T3 template<class T1, class T2, class T3> #define TT1T2T3T4 template<class T1, class T2, class T3, class T4> #define TRT1T2T3 triple<T1, T2, T3> TT1T2T3 struct triple { T1 x; T2 y; T3 z; triple() : x(T1()), y(T2()), z(T3()) {}; triple(T1 _x, T2 _y, T3 _z) :x(_x), y(_y), z(_z) {} }; TT1T2T3 bool operator<(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.x != t2.x)return t1.x < t2.x; if (t1.y != t2.y) return t1.y < t2.y; else return t1.z < t2.z; } TT1T2T3 bool operator>(const TRT1T2T3&t1, const TRT1T2T3&t2) { if (t1.x != t2.x)return t1.x > t2.x; if (t1.y != t2.y) return t1.y > t2.y; else return t1.z > t2.z; } TT1T2T3 bool operator==(const TRT1T2T3&t1, const TRT1T2T3&t2) { return (t1.x == t2.x && t1.y == t2.y && t1.z == t2.z); } TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.x >> t.y >> t.y; } TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t) { return os << t.x << " " << t.y << " " << t.z; } TT1T2T3 TRT1T2T3 operator+(TRT1T2T3 a, TRT1T2T3 b) { return { a.x + b.x, a.y + b.y, a.z + b.z }; } TT1T2T3 TRT1T2T3 operator-(TRT1T2T3 a, TRT1T2T3 b) { return { a.x - b.x, a.y - b.y, a.z - b.z }; } TT1T2T3T4 TRT1T2T3 operator*(TRT1T2T3 a, T4 val) { return { a.x * val, a.y * val, a.z * val }; } TT1T2T3T4 TRT1T2T3 operator/(TRT1T2T3 a, T4 val) { return { a.x / val, a.y / val, a.z / val }; } #undef TT1T2T3T4 #undef TRT1T2T3 #undef TT1T2T3 using TRII = triple<int, int, int>; using TRLL = triple<LL, LL, LL>; using TRLD = triple<LD, LD, LD>; using VTRII = vector<TRII>; using VTRLL = vector<TRLL>; using VTRLD = vector<TRLD>; } using namespace triples; namespace geo { template<class T> T dotProduct(pair<T, T> a, pair<T, T> b) { return a.first*b.first + a.second* b.second; } template<class T> T crossProduct(pair<T, T>a, pair<T, T> b) { return a.first * b.second - a.second * b.first; } template<class T> T lengthPow(pair<T, T> a) { return a.first*1ll*a.first + a.second*1ll*a.second; } template<class T> LD length(pair<T, T> a) { return sqrt(lengthPow(a)); } template<class T> T dotProduct(triple<T, T, T> a, triple<T, T, T> b) { return a.x*b.x + a.y* b.y + a.z*b.z; } template<class T> T crossProduct(triple<T, T, T> a, triple<T, T, T> b) { return { a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x }; } template<class T> T lengthPow(triple<T, T, T> a) { return a.x*1ll*a.x + a.y*1ll*a.y + a.z*1ll*a.z; } template<class T> LD length(triple<T, T, T> a) { return sqrt(lengthPow(a)); } } using namespace geo; template <class T> T invGeneral(T a, T b) { // 0 < a < b, gcd(a,b) = 1 a %= b; if (a == 0) return b == 1 ? 0 : -1; T x = invGeneral(b, a); return x == -1 ? -1 : ((1 - (LL)b * x) / a + b) % b; } template<class T> struct modular { T val; explicit operator T() const { return val; } modular() { val = 0; } modular(const LL& v) { val = (-MOD <= v && v <= MOD) ? v : v % MOD; if (val < 0) val += MOD; } friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; } friend void pr(const modular& a) { pr(a.val); } friend void re(modular& a) { LL x; re(x); a = modular(x); } friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; } friend bool operator!=(const modular& a, const modular& b) { return !(a == b); } friend bool operator<(const modular& a, const modular& b) { return a.val < b.val; } modular operator-() const { return modular(-val); } modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; } modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; } modular& operator*=(const modular& m) { val = (LL)val*m.val%MOD; return *this; } friend modular pow(modular a, LL p) { modular ans = 1; for (; p; p /= 2, a *= a) if (p & 1) ans *= a; return ans; } friend modular inv(const modular& a) { auto i = invGeneral(a.val, MOD); assert(i != -1); return i; } // equivalent to return exp(b,MOD-2) if MOD is prime modular& operator/=(const modular& m) { return (*this) *= inv(m); } friend modular operator+(modular a, const modular& b) { return a += b; } friend modular operator-(modular a, const modular& b) { return a -= b; } friend modular operator*(modular a, const modular& b) { return a *= b; } friend modular operator/(modular a, const modular& b) { return a /= b; } }; using MI = modular<int>; using PMI = pair<MI, MI>; using VMI = vector<MI>; using VPMI = vector<PMI>; namespace debug { template < typename _T > inline void _debug(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template < typename _T, typename... args > void _debug(const char *s, _T x, args... a) { while (*s != ',') cerr << *s++; cerr << " = " << x << ','; _debug(s + 1, a...); } #if 0 && defined(LOCAL) #define debug(...) _debug(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) 1999 #define cerr if(0) cout #endif } using namespace debug; template <class T> bool setMax(T& v, T newV) { if (v < newV) { v = newV; return true; } return false; } template <class T> bool setMin(T& v, T newV) { if (v > newV) { v = newV; return true; } return false; } int calc(const vector<int>& lengths, const vector<int>& ends, int pos) { if(pos < 0) return 0; if(pos > lengths.size()) return 0; int res = 0; int cnt = 0; for(int i = 0; i < pos; ++i){ res += lengths[i] - (2 * cnt + 1); cnt += 2; } cerr << "co sie dzieje " << cnt << ' ' << res << '\n'; for(auto u : ends) { if(u > cnt) { res += u - cnt; ++cnt; } } cerr << "konce done " << cnt << ' ' << res << '\n'; while(cnt < lengths.size() and lengths[pos] > 2 * cnt) { cerr << "OKKK\n"; cerr << lengths[pos] << ' ' << cnt << '\n'; res++; cnt++; } return res; } void solve() { int n; cin >> n; string s; cin >> s; cerr << "tescior\n"; int beg = -1; vector<int> lengths; int last = -1; for(int i = 0; i < n; ++i) { if(s[i] == '0') continue; if(beg == -1) beg = i; if(last != -1 and last < i - 1) lengths.push_back(i - last - 1); last = i; } vector<int> ends = {beg, n - last - 1}; sort(ends.begin(), ends.end(), greater<int>()); sort(lengths.begin(), lengths.end(), greater<int>()); vector<int> dp = {0, ends[0], ends[0] + max(0, ends[1] - 1)}; int pos = 0; for(auto u : lengths) { vector<int> newDp(3); newDp[0] = dp[0] + max({0, u - (2 * 2 * pos + 1), min(1, u - 2 * 2 * pos)}); newDp[1] = dp[1] + max({0, u - (2 * (2 * pos + 1) + 1), min(1, u - 2 * (2 * pos + 1))}); newDp[2] = dp[2] + max({0, u - (2 * (2 * pos + 2) + 1), min(1, u - 2 * (2 * pos + 2))}); newDp[1] = max(newDp[1], dp[0] + max(0, ends[0] - 2 * pos)); newDp[2] = max(newDp[2], dp[1] + max(0, ends[1] - (2 * pos + 1))); newDp[2] = max(newDp[2], dp[0] + max(0, ends[0] - 2 * pos) + max(0, ends[1] - (2 * pos + 1))); swap(newDp, dp); ++pos; } cout << n - max({dp[0], dp[1], dp[2]}) << '\n'; } int main() { // #ifndef LOCAL // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); // #endif BOOST; cout << fixed << setprecision(13); int t = 1; cerr << "testy!!!!\n"; cin >> t; for(int i = 1; i <= t; ++i) // string s; // while(cin >> s) { // cout << "Case #" << i << ": "; solve(); } return 0; } |