/*
PANDEMIA
ERYK MARCINIAK B)
*/
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
int ile;
void solve()
{
bool bul=0;
char a;
int n=0,wyn=0,dp=0;
vector< pair<int,int> >t;
cin >> n;
cin >> a;
if(a=='0')
{
dp=1;
bul=1;
}
for(int i=1;i<n;i++)
{
cin >> a;
if(a=='0')
dp++;
else if(dp)
{
if(!bul)
t.push_back({dp,0});
else
t.push_back({dp*2,1});
bul=0;
dp=0;
}
}
if(dp)
t.push_back({dp*2,1});
sort(t.rbegin(),t.rend());
/*for(int i=0;i<t.size();i++)
{
cout << t[i].fi << " " << endl;
}*/
int time=0;
for(int i=0;i<t.size();i++)
{
if(t[i].se==1 && t[i].fi/2-time>0)
{
wyn=wyn+(t[i].fi/2-time);
time++;
//cout << "a" << t[i].fi/2-time << endl;
}
else
{
int pom;
if(t[i].fi-(time*2)==1) pom=1;
else pom=t[i].fi-(time*2)-1;
if(pom>0)
{
if(pom==1)
time--;
time+=2;
wyn+=pom;
//cout << "pom" << pom << endl;
//cout << "b" << wyn << endl;
}
else break;
}
}
//cout << endl << n << endl << wyn << endl;
cout << n-wyn << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> ile;
for(int i=0;i<ile;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 | /* PANDEMIA ERYK MARCINIAK B) */ #include <bits/stdc++.h> #define fi first #define se second using namespace std; int ile; void solve() { bool bul=0; char a; int n=0,wyn=0,dp=0; vector< pair<int,int> >t; cin >> n; cin >> a; if(a=='0') { dp=1; bul=1; } for(int i=1;i<n;i++) { cin >> a; if(a=='0') dp++; else if(dp) { if(!bul) t.push_back({dp,0}); else t.push_back({dp*2,1}); bul=0; dp=0; } } if(dp) t.push_back({dp*2,1}); sort(t.rbegin(),t.rend()); /*for(int i=0;i<t.size();i++) { cout << t[i].fi << " " << endl; }*/ int time=0; for(int i=0;i<t.size();i++) { if(t[i].se==1 && t[i].fi/2-time>0) { wyn=wyn+(t[i].fi/2-time); time++; //cout << "a" << t[i].fi/2-time << endl; } else { int pom; if(t[i].fi-(time*2)==1) pom=1; else pom=t[i].fi-(time*2)-1; if(pom>0) { if(pom==1) time--; time+=2; wyn+=pom; //cout << "pom" << pom << endl; //cout << "b" << wyn << endl; } else break; } } //cout << endl << n << endl << wyn << endl; cout << n-wyn << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> ile; for(int i=0;i<ile;i++) solve(); return 0; } |
English