#include<cstdio>
#include<algorithm>
using namespace std;
struct zdr{
int x;
bool prawo;
int w;
bool operator<(zdr a)const{
if(w != a.w)
return w < a.w;
else{
return x < a.x;
}
}
};
static zdr tab[100010];
static int wsk = 0;
int main(){
int t;
scanf("%i", &t);
while(t--){
int n;
scanf("%i", &n);
int curr = 0;
for(int i = 0; i < n; ++i){
char z;
scanf(" %c", &z);
if(z == '1'){
if(wsk){
tab[wsk].w = curr;
tab[wsk].x = i - curr;
tab[wsk++].prawo = true;
tab[wsk].w = curr;
tab[wsk].x = i - 1;
tab[wsk++].prawo = false;
}
else{
tab[wsk].w = curr * 2;
tab[wsk].x = i - 1;
tab[wsk++].prawo = false;
}
curr = 0;
}
else{
curr++;
}
}
tab[wsk].w = 2 * curr;
tab[wsk].x = n - curr;
tab[wsk++].prawo = true;
if(wsk == 1){
puts("0");
return 0;
}
sort(tab, tab+wsk);
int tura = 0;
int uratowane = 0;
while(wsk){
--wsk;
if(2 * tura >= tab[wsk].w)
break;
uratowane += tab[wsk].w - 2 * tura;
++tura;
}
printf("%i\n", n-uratowane/2);
}
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 | #include<cstdio> #include<algorithm> using namespace std; struct zdr{ int x; bool prawo; int w; bool operator<(zdr a)const{ if(w != a.w) return w < a.w; else{ return x < a.x; } } }; static zdr tab[100010]; static int wsk = 0; int main(){ int t; scanf("%i", &t); while(t--){ int n; scanf("%i", &n); int curr = 0; for(int i = 0; i < n; ++i){ char z; scanf(" %c", &z); if(z == '1'){ if(wsk){ tab[wsk].w = curr; tab[wsk].x = i - curr; tab[wsk++].prawo = true; tab[wsk].w = curr; tab[wsk].x = i - 1; tab[wsk++].prawo = false; } else{ tab[wsk].w = curr * 2; tab[wsk].x = i - 1; tab[wsk++].prawo = false; } curr = 0; } else{ curr++; } } tab[wsk].w = 2 * curr; tab[wsk].x = n - curr; tab[wsk++].prawo = true; if(wsk == 1){ puts("0"); return 0; } sort(tab, tab+wsk); int tura = 0; int uratowane = 0; while(wsk){ --wsk; if(2 * tura >= tab[wsk].w) break; uratowane += tab[wsk].w - 2 * tura; ++tura; } printf("%i\n", n-uratowane/2); } return 0; } |
English