#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef uint32_t ul;
typedef int32_t l;
typedef uint64_t ull;
typedef int64_t ll;
const l INF = 1000000000 + 9;
const ll MOD = 123456789;
const l PRIME = 200003;
const ll L_INF = 1000000000000000000LL + 7;
const double EPS = 1e-5;
#define FOR(x, y, z) for (ll z = x; z < y; z++)
#define FORE(x, y, z) for (ll z = x; z <= y; z++)
#define FORD(x, y, z) for (ll z = x; z > y; z--)
#define FORDE(x, y, z) for (ll z = x; z >= y; z--)
#define REP(x, y) for (ll y = 0; y < x; y++)
#define ALL(...) (__VA_ARGS__).begin(), (__VA_ARGS__).end()
#define PF push_front
#define POF pop_front
#define PB push_back
#define POB pop_back
#define MP make_pair
#define FS first
#define SC second
ll n, num_tests, cnt=1;
ll best = 0;
char last = 'o';
bool first = true;
vector<ll> one, two;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin >> num_tests;
while(num_tests--)
{
best = 0, cnt = 1;
last = 'o';
first = true;
one.clear(), two.clear();
cin >> n;
FOR(0, n+1, i)
{
char a;
if(i < n)
cin >> a;
else
a = '1';
if(i != 0)
{
if(a != last && a == '1')
{
if(first || i == n)
{
one.PB(cnt);
first = false;
}
else
two.PB(cnt);
cnt = 1;
}
else if(a != last && a == '0')
cnt = 1;
else
cnt++;
}
else if(i == 0 && a == '1')
first = false;
last = a;
}
sort(ALL(one), greater<ll>());
sort(ALL(two), greater<ll>());
/*for(auto u: one)
cout << u << " ";
cout << "\n------------\n";
for(auto u: two)
cout << u << " ";
cout << "\n------------\n";*/
ll curr = 0, n_curr;
FORE(0, (ll)one.size(), i)
{
if(i)
curr += max(one[i-1]-(i-1LL), 0LL);
n_curr = curr;
FORE(0, (ll)two.size(), j)
{
if(j)
if(two[j-1]-2LL*(i+2LL*(j-1LL)) > 0)
n_curr += max(two[j-1]-2LL*(i+2LL*(j-1LL))-1LL-1LL, 0LL)+1LL;
/*if(n_curr > best)
cout << i << " " << j << "\n";*/
best = max(best, n_curr);
}
}
cout << n-best << "\n";
}
}
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 | #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef uint32_t ul; typedef int32_t l; typedef uint64_t ull; typedef int64_t ll; const l INF = 1000000000 + 9; const ll MOD = 123456789; const l PRIME = 200003; const ll L_INF = 1000000000000000000LL + 7; const double EPS = 1e-5; #define FOR(x, y, z) for (ll z = x; z < y; z++) #define FORE(x, y, z) for (ll z = x; z <= y; z++) #define FORD(x, y, z) for (ll z = x; z > y; z--) #define FORDE(x, y, z) for (ll z = x; z >= y; z--) #define REP(x, y) for (ll y = 0; y < x; y++) #define ALL(...) (__VA_ARGS__).begin(), (__VA_ARGS__).end() #define PF push_front #define POF pop_front #define PB push_back #define POB pop_back #define MP make_pair #define FS first #define SC second ll n, num_tests, cnt=1; ll best = 0; char last = 'o'; bool first = true; vector<ll> one, two; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> num_tests; while(num_tests--) { best = 0, cnt = 1; last = 'o'; first = true; one.clear(), two.clear(); cin >> n; FOR(0, n+1, i) { char a; if(i < n) cin >> a; else a = '1'; if(i != 0) { if(a != last && a == '1') { if(first || i == n) { one.PB(cnt); first = false; } else two.PB(cnt); cnt = 1; } else if(a != last && a == '0') cnt = 1; else cnt++; } else if(i == 0 && a == '1') first = false; last = a; } sort(ALL(one), greater<ll>()); sort(ALL(two), greater<ll>()); /*for(auto u: one) cout << u << " "; cout << "\n------------\n"; for(auto u: two) cout << u << " "; cout << "\n------------\n";*/ ll curr = 0, n_curr; FORE(0, (ll)one.size(), i) { if(i) curr += max(one[i-1]-(i-1LL), 0LL); n_curr = curr; FORE(0, (ll)two.size(), j) { if(j) if(two[j-1]-2LL*(i+2LL*(j-1LL)) > 0) n_curr += max(two[j-1]-2LL*(i+2LL*(j-1LL))-1LL-1LL, 0LL)+1LL; /*if(n_curr > best) cout << i << " " << j << "\n";*/ best = max(best, n_curr); } } cout << n-best << "\n"; } } |
English