#include <bits/stdc++.h>
using namespace std;
vector <int> odp;
bool comp(int a, int b)
{
if (a>b) return 1;
return 0;
}
int main()
{
// ios_base::sync_with_stdio(0);
int n;
scanf("%d", &n);
//cin>>n;
for(int i=0; i<n; ++i)
{
int a, k=0;
bool czy1=0;
vector <int> tab;
scanf("%d", &a);
//cin>>a;
getchar();
for(int j=0; j<a; ++j)
{
char c;
c=getchar();
//cin>>c;
if (czy1==0 && c=='0')
{
k++;
continue;
}
if (czy1==0 && c=='1')
{
if (k!=0)
tab.push_back(k);
k=0;
czy1=1;
continue;
}
if (c=='0')
{
++k;
continue;
}
if (c=='1' && k!=0)
{
if (k%2==0)
{
tab.push_back(k/2);
tab.push_back(k/2);
}
else
{
tab.push_back(k/2);
tab.push_back(k/2+1);
}
k=0;
continue;
}
}
if (k!=0)
{
tab.push_back(k);
}
if (czy1==0)
{
odp.push_back(0);
continue;
}
sort(tab.begin(), tab.end(), comp);
int count=0;
for(int i=0; i<tab.size(); ++i)
{
//cout<<tab[i]<<' ';
if (tab[i]-i<=0) break;
count+=tab[i]-i;
}
//cout<<"\n";
odp.push_back(a-count);
}
for(auto &i:odp)
{
printf("%d\n", i);
//cout<<i<<"\n";
}
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 | #include <bits/stdc++.h> using namespace std; vector <int> odp; bool comp(int a, int b) { if (a>b) return 1; return 0; } int main() { // ios_base::sync_with_stdio(0); int n; scanf("%d", &n); //cin>>n; for(int i=0; i<n; ++i) { int a, k=0; bool czy1=0; vector <int> tab; scanf("%d", &a); //cin>>a; getchar(); for(int j=0; j<a; ++j) { char c; c=getchar(); //cin>>c; if (czy1==0 && c=='0') { k++; continue; } if (czy1==0 && c=='1') { if (k!=0) tab.push_back(k); k=0; czy1=1; continue; } if (c=='0') { ++k; continue; } if (c=='1' && k!=0) { if (k%2==0) { tab.push_back(k/2); tab.push_back(k/2); } else { tab.push_back(k/2); tab.push_back(k/2+1); } k=0; continue; } } if (k!=0) { tab.push_back(k); } if (czy1==0) { odp.push_back(0); continue; } sort(tab.begin(), tab.end(), comp); int count=0; for(int i=0; i<tab.size(); ++i) { //cout<<tab[i]<<' '; if (tab[i]-i<=0) break; count+=tab[i]-i; } //cout<<"\n"; odp.push_back(a-count); } for(auto &i:odp) { printf("%d\n", i); //cout<<i<<"\n"; } return 0; } |
English