#include<bits/stdc++.h> using namespace std; int tests,n,times,roz,Suma; vector< pair<int,int> > vec; vector < int > v1,one,two; priority_queue<int> q; int wyp; void aktualizuj(); int aktw(); int main() { ios_base::sync_with_stdio(0); cin >> tests; for (int test = 1 ; test <= tests ; test++) { cin >> n; int last_1 = 0; for (int i = 1 ; i <= n ; i++) { char c; cin >> c; if(c == '1') { v1.push_back(i); } } v1.push_back(n+1); if(v1.size() == 1) cout << 0<<"\n"; else { for (int i = 0 ; i < v1.size() ; i++) { if(i == v1.size() - 1) { if(v1[i-1] != n) vec.push_back({n - v1[i-1],1}); } else if(i == 0) { if(v1[i] != 1) vec.push_back({v1[i] - 1,1}); } else { if(v1[i] - v1[i-1] > 1) vec.push_back({v1[i] - v1[i-1] - 1,2}); } } for(int i = 0 ; i < vec.size(); i++) { if(vec[i].second == 1) one.push_back(vec[i].first); else two.push_back(vec[i].first); } int Odp = INT_MAX; sort(two.begin(),two.end()); for(int i = 0 ; i < one.size() ; i++) { q.push(-one[i]); Suma += one[i]; roz++; } for(int i = two.size() - 1 ; i >= 0 ; i--) { aktualizuj(); if(1 + 2*times + 2*roz <= two[i]) { Odp = min(Odp,n - (aktw() + 1)); Suma += (two[i] - times); q.push(-(two[i] - times)); times++; roz++; Odp = min(Odp,n - aktw()); } else Odp = min(Odp,n - aktw()); } if (wyp == 0) { aktualizuj(); Odp = min(Odp,n - aktw()); } cout << Odp<<"\n"; } Suma = 0; two.clear(); one.clear(); v1.clear(); vec.clear(); while(!q.empty()) q.pop(); roz = 0; times = 0; } } int aktw() { return (Suma - times * roz - (roz * (roz-1)) / 2); } void aktualizuj() { while(!q.empty()) { if(Suma - times * roz - (roz * (roz-1)) / 2 <= Suma + q.top() - times*(roz-1) - ((roz-2) * (roz-1)) / 2) { Suma += q.top(); q.pop(); roz--; } else break; } }
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 | #include<bits/stdc++.h> using namespace std; int tests,n,times,roz,Suma; vector< pair<int,int> > vec; vector < int > v1,one,two; priority_queue<int> q; int wyp; void aktualizuj(); int aktw(); int main() { ios_base::sync_with_stdio(0); cin >> tests; for (int test = 1 ; test <= tests ; test++) { cin >> n; int last_1 = 0; for (int i = 1 ; i <= n ; i++) { char c; cin >> c; if(c == '1') { v1.push_back(i); } } v1.push_back(n+1); if(v1.size() == 1) cout << 0<<"\n"; else { for (int i = 0 ; i < v1.size() ; i++) { if(i == v1.size() - 1) { if(v1[i-1] != n) vec.push_back({n - v1[i-1],1}); } else if(i == 0) { if(v1[i] != 1) vec.push_back({v1[i] - 1,1}); } else { if(v1[i] - v1[i-1] > 1) vec.push_back({v1[i] - v1[i-1] - 1,2}); } } for(int i = 0 ; i < vec.size(); i++) { if(vec[i].second == 1) one.push_back(vec[i].first); else two.push_back(vec[i].first); } int Odp = INT_MAX; sort(two.begin(),two.end()); for(int i = 0 ; i < one.size() ; i++) { q.push(-one[i]); Suma += one[i]; roz++; } for(int i = two.size() - 1 ; i >= 0 ; i--) { aktualizuj(); if(1 + 2*times + 2*roz <= two[i]) { Odp = min(Odp,n - (aktw() + 1)); Suma += (two[i] - times); q.push(-(two[i] - times)); times++; roz++; Odp = min(Odp,n - aktw()); } else Odp = min(Odp,n - aktw()); } if (wyp == 0) { aktualizuj(); Odp = min(Odp,n - aktw()); } cout << Odp<<"\n"; } Suma = 0; two.clear(); one.clear(); v1.clear(); vec.clear(); while(!q.empty()) q.pop(); roz = 0; times = 0; } } int aktw() { return (Suma - times * roz - (roz * (roz-1)) / 2); } void aktualizuj() { while(!q.empty()) { if(Suma - times * roz - (roz * (roz-1)) / 2 <= Suma + q.top() - times*(roz-1) - ((roz-2) * (roz-1)) / 2) { Suma += q.top(); q.pop(); roz--; } else break; } } |