#include <iostream> #include <vector> #include <algorithm> #include <functional> #include <iterator> #include <bitset> #include <string> #include <utility> void splitToScetors(std::vector<std::pair<int,bool> > §ors, const std::string& s) { int len=0; bool start = true; for(const auto& x:s) { if(x=='1') { if(len) sectors.push_back({len, start}); start = false; len = 0; } else { ++len; } } if(len) sectors.push_back({len, true}); } int calculateNumberOfOK(std::vector<std::pair<int,bool> > §ors) { std::sort(sectors.begin(), sectors.end(), [](const std::pair<int,bool> &a, const std::pair<int,bool> &b) -> bool { int prioA = a.second? 2*a.first:a.first; int prioB = b.second? 2*b.first:b.first; return prioA > prioB; }); int safe =0, t=0; for(const auto& x:sectors) { if(x.first < t) break; if(x.second) { if(x.first > t) safe += x.first -t; t++; } else { if(x.first -2*t ==1) ++safe; else if(x.first > (2*t +1)) safe += x.first -2*t-1; t++; t++; } } return safe; } int main() { std::ios_base::sync_with_stdio(false); long long int t; std::cin>>t; for(int i=0; i<t; ++i) { long long int n; std::string s; std::cin>>n>>s; std::vector<std::pair<int,bool>> sectors; splitToScetors(sectors, s); int numb = n - calculateNumberOfOK(sectors); std::cout << numb <<std::endl; } 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 | #include <iostream> #include <vector> #include <algorithm> #include <functional> #include <iterator> #include <bitset> #include <string> #include <utility> void splitToScetors(std::vector<std::pair<int,bool> > §ors, const std::string& s) { int len=0; bool start = true; for(const auto& x:s) { if(x=='1') { if(len) sectors.push_back({len, start}); start = false; len = 0; } else { ++len; } } if(len) sectors.push_back({len, true}); } int calculateNumberOfOK(std::vector<std::pair<int,bool> > §ors) { std::sort(sectors.begin(), sectors.end(), [](const std::pair<int,bool> &a, const std::pair<int,bool> &b) -> bool { int prioA = a.second? 2*a.first:a.first; int prioB = b.second? 2*b.first:b.first; return prioA > prioB; }); int safe =0, t=0; for(const auto& x:sectors) { if(x.first < t) break; if(x.second) { if(x.first > t) safe += x.first -t; t++; } else { if(x.first -2*t ==1) ++safe; else if(x.first > (2*t +1)) safe += x.first -2*t-1; t++; t++; } } return safe; } int main() { std::ios_base::sync_with_stdio(false); long long int t; std::cin>>t; for(int i=0; i<t; ++i) { long long int n; std::string s; std::cin>>n>>s; std::vector<std::pair<int,bool>> sectors; splitToScetors(sectors, s); int numb = n - calculateNumberOfOK(sectors); std::cout << numb <<std::endl; } return 0; } |