//#pragma GCC optimize("Ofast") //#pragma GCC optimize ("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma warning(disable:4786) #pragma warning(disable:4996) #include <random> #include <chrono> #include <ctime> #include<list> #include <numeric> #include<bitset> #include<iostream> #include<cstdio> #include<algorithm> #include<vector> #include<set> #include<map> #include<functional> #include<string> #include<cstring> #include<cstdlib> #include<queue> #include<utility> #include<fstream> #include<sstream> #include<cmath> #include<stack> #include<assert.h> #include<unordered_map> #include<unordered_set> #include <array> #include <complex> #include<iomanip> using namespace std; #define MEM(a, b) memset(a, (b), sizeof(a)) #define CLR(a) memset(a, 0, sizeof(a)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) ) #define S(X) ( (X) * (X) ) #define SZ(V) (int )V.size() #define FORN(i, n) for(int i = 0; i < n; i++) #define FORAB(i, a, b) for(int i = a; i <= b; i++) #define ALL(V) V.begin(), V.end() #define IN(A, B, C) ((B) <= (A) && (A) <= (C)) #define AIN(A, B, C) assert(IN(A, B, C)) typedef long long int LL; //typedef __int128 LLL; typedef long long LLL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<LL, int> PLI; typedef pair<double, double> PDD; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PLL> VPL; typedef vector<PII> VP; typedef vector<double> VD; typedef vector<vector<int>> VVI; typedef vector<string> VS; typedef long double ld; typedef unsigned long long ULL; //#define MAXN 1000 //#define MAXN2 MAXN*MAXN #define MAXN 100 //const LL MOD[2] = { 87420317, 1000000007 }; const LL MOD = 1000000007; //const LL MOD = 998244353; //const LL MOD = 547892069; //const LL INF = 2000000000000000001LL; //2e18 + 1 int n; char S[100005]; bool cmp(PII & A, PII & B) { if (A.second != B.second) return A.second < B.second; return A.first > B.first; } int compute(const VP& V) { int sum = 0; int d = 0; FORN(i, SZ(V)) { if (V[i].second == 1) { sum += MAX(0, V[i].first - d); d++; } else { sum += MAX(0, (V[i].first - d * 2 == 1 ? 1 : V[i].first - d * 2 - 1)); d += 2; } } return sum; } void solve(int ks) { VP V; VP side; scanf("%d %s", &n, S); int run = 0; int start = 1; for (int i = 0; i < n; i++) { if (S[i] == '1') { if (start) side.push_back({ run, 1 }); else V.push_back({ run, 2 }); start = 0; run = 0; } else { run++; } } side.push_back({ run, 1 }); sort(ALL(V), cmp); sort(ALL(side), cmp); int save = 0; save = compute(V); if (side.size() > 0) { VP Z = { side[0] }; for (auto& p : V) Z.push_back(p); int cur = compute(Z); save = MAX(save, cur); } if (side.size() > 1) { VP Z = { side[0], side[1] }; for (auto& p : V) Z.push_back(p); int cur = compute(Z); save = MAX(save, cur); } printf("%d\n", n - save); } void gen() { } int main() { double start_time = clock(); #ifdef LOCAL freopen("C:\\Home\\Contests\\F\\sample.in", "r", stdin); //freopen("C:\\Home\\Contests\\F\\0.out", "w", stdout); #endif gen(); if (1) { int T; scanf("%d", &T); //AIN(T, 1, 10); for (int ks = 1; ks <= T; ks++) { solve(ks); //if (ks % 1 == 0) fprintf(stderr, "%d done\n", ks); } } else { solve(1); } double end_time = clock(); fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC); 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 | //#pragma GCC optimize("Ofast") //#pragma GCC optimize ("unroll-loops") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma warning(disable:4786) #pragma warning(disable:4996) #include <random> #include <chrono> #include <ctime> #include<list> #include <numeric> #include<bitset> #include<iostream> #include<cstdio> #include<algorithm> #include<vector> #include<set> #include<map> #include<functional> #include<string> #include<cstring> #include<cstdlib> #include<queue> #include<utility> #include<fstream> #include<sstream> #include<cmath> #include<stack> #include<assert.h> #include<unordered_map> #include<unordered_set> #include <array> #include <complex> #include<iomanip> using namespace std; #define MEM(a, b) memset(a, (b), sizeof(a)) #define CLR(a) memset(a, 0, sizeof(a)) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define ABS(X) ( (X) > 0 ? (X) : ( -(X) ) ) #define S(X) ( (X) * (X) ) #define SZ(V) (int )V.size() #define FORN(i, n) for(int i = 0; i < n; i++) #define FORAB(i, a, b) for(int i = a; i <= b; i++) #define ALL(V) V.begin(), V.end() #define IN(A, B, C) ((B) <= (A) && (A) <= (C)) #define AIN(A, B, C) assert(IN(A, B, C)) typedef long long int LL; //typedef __int128 LLL; typedef long long LLL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef pair<LL, int> PLI; typedef pair<double, double> PDD; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<PLL> VPL; typedef vector<PII> VP; typedef vector<double> VD; typedef vector<vector<int>> VVI; typedef vector<string> VS; typedef long double ld; typedef unsigned long long ULL; //#define MAXN 1000 //#define MAXN2 MAXN*MAXN #define MAXN 100 //const LL MOD[2] = { 87420317, 1000000007 }; const LL MOD = 1000000007; //const LL MOD = 998244353; //const LL MOD = 547892069; //const LL INF = 2000000000000000001LL; //2e18 + 1 int n; char S[100005]; bool cmp(PII & A, PII & B) { if (A.second != B.second) return A.second < B.second; return A.first > B.first; } int compute(const VP& V) { int sum = 0; int d = 0; FORN(i, SZ(V)) { if (V[i].second == 1) { sum += MAX(0, V[i].first - d); d++; } else { sum += MAX(0, (V[i].first - d * 2 == 1 ? 1 : V[i].first - d * 2 - 1)); d += 2; } } return sum; } void solve(int ks) { VP V; VP side; scanf("%d %s", &n, S); int run = 0; int start = 1; for (int i = 0; i < n; i++) { if (S[i] == '1') { if (start) side.push_back({ run, 1 }); else V.push_back({ run, 2 }); start = 0; run = 0; } else { run++; } } side.push_back({ run, 1 }); sort(ALL(V), cmp); sort(ALL(side), cmp); int save = 0; save = compute(V); if (side.size() > 0) { VP Z = { side[0] }; for (auto& p : V) Z.push_back(p); int cur = compute(Z); save = MAX(save, cur); } if (side.size() > 1) { VP Z = { side[0], side[1] }; for (auto& p : V) Z.push_back(p); int cur = compute(Z); save = MAX(save, cur); } printf("%d\n", n - save); } void gen() { } int main() { double start_time = clock(); #ifdef LOCAL freopen("C:\\Home\\Contests\\F\\sample.in", "r", stdin); //freopen("C:\\Home\\Contests\\F\\0.out", "w", stdout); #endif gen(); if (1) { int T; scanf("%d", &T); //AIN(T, 1, 10); for (int ks = 1; ks <= T; ks++) { solve(ks); //if (ks % 1 == 0) fprintf(stderr, "%d done\n", ks); } } else { solve(1); } double end_time = clock(); fprintf(stderr, "Time = %lf\n", (end_time - start_time) / CLOCKS_PER_SEC); return 0; } |