#include <bits/stdc++.h>
using namespace std;
struct dane
{
long long czas, wartosc, zmniejsz;
};
vector <dane> tab;
bool cmp(dane a, dane b)
{
if(a.wartosc<b.wartosc)
return 1;
return 0;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n, m, zero=0, t=0, pom=0, wynik=0;
string slowo;
cin>>m;
for(int j=0;j<m;j++)
{
zero=0;
t=0;
pom=0;
wynik=0;
cin>>n;
cin>>slowo;
for(int i=0; i<n; i++)
{
if(slowo[i]=='0')
zero++;
else if(zero!=0)
{
tab.push_back ({(zero+1)/2,zero,2});
zero=0;
}
else
zero=0;
}
if(zero!=0)
{
tab.push_back ({zero,zero,1});
zero=0;
}
if(slowo[0]=='0')
{
tab[0].czas=tab[0].wartosc;
tab[0].zmniejsz=1;
}
sort(tab.begin(), tab.end(),cmp);
/*for(int i=0; i<tab.size(); i++)
cout<<tab[i].czas<<" "<<tab[i].wartosc<<" "<<tab[i].zmniejsz<<endl;*/
for(int i=tab.size()-1;i>=0;i--)
{
if(tab[i].wartosc>t*tab[i].zmniejsz)
{
wynik=wynik+tab[i].wartosc-t*tab[i].zmniejsz;
if(tab[i].zmniejsz==2)
wynik--;
t+=tab[i].zmniejsz;
}
}
cout<<n-wynik<<endl;
tab.clear();
}
}
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 | #include <bits/stdc++.h> using namespace std; struct dane { long long czas, wartosc, zmniejsz; }; vector <dane> tab; bool cmp(dane a, dane b) { if(a.wartosc<b.wartosc) return 1; return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, zero=0, t=0, pom=0, wynik=0; string slowo; cin>>m; for(int j=0;j<m;j++) { zero=0; t=0; pom=0; wynik=0; cin>>n; cin>>slowo; for(int i=0; i<n; i++) { if(slowo[i]=='0') zero++; else if(zero!=0) { tab.push_back ({(zero+1)/2,zero,2}); zero=0; } else zero=0; } if(zero!=0) { tab.push_back ({zero,zero,1}); zero=0; } if(slowo[0]=='0') { tab[0].czas=tab[0].wartosc; tab[0].zmniejsz=1; } sort(tab.begin(), tab.end(),cmp); /*for(int i=0; i<tab.size(); i++) cout<<tab[i].czas<<" "<<tab[i].wartosc<<" "<<tab[i].zmniejsz<<endl;*/ for(int i=tab.size()-1;i>=0;i--) { if(tab[i].wartosc>t*tab[i].zmniejsz) { wynik=wynik+tab[i].wartosc-t*tab[i].zmniejsz; if(tab[i].zmniejsz==2) wynik--; t+=tab[i].zmniejsz; } } cout<<n-wynik<<endl; tab.clear(); } } |
English