#include<cstdio> #include<cmath> #include<vector> #include<iostream> #include<string> #include<algorithm> using namespace std; int main(){ int n,t; char c; scanf("%d", &t); for( int i =0; i < t; i++){ scanf("%d", &n); char cities[n]; scanf("%s", cities); vector<pair<int,int> > tmp; int good = 0; char city = '0'; bool prev_good = false; bool any_bad_before = false; for( int j=0; j < n; j++){ city = cities[j]; if (city == '0'){ good++; prev_good = true; if ( j == n-1 ){ if ( any_bad_before == false ){ tmp.push_back(make_pair(good, 0)); } else{ tmp.push_back(make_pair(good, 1)); } } continue; } else{ if (prev_good == true){ prev_good = false; if ( any_bad_before == false){ tmp.push_back(make_pair(good, 1)); good=0; any_bad_before = true; } else{ tmp.push_back(make_pair(good, 2)); good=0; } } else{ any_bad_before = true; continue; } } } if (tmp.size() == 1 && tmp[0].second == 0){ printf("%d\n", 0); continue; } sort(tmp.begin(), tmp.end(), [](auto a, auto b) { return float(a.first) / a.second > float(b.first) / b.second; }); int day = 0; long long int result = 0; for( auto g : tmp){ if ( g.first > day * g.second){ if (g.second == 0){ result += g.first; } else if(g.second == 1){ result += g.first - day; day++; } else{ result += 1; if (g.first - 2 *day > 2){ result += g.first - 2 *day -2; day++; } day++; } } else break; } printf("%d\n", n -result); } 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 92 93 94 95 96 97 | #include<cstdio> #include<cmath> #include<vector> #include<iostream> #include<string> #include<algorithm> using namespace std; int main(){ int n,t; char c; scanf("%d", &t); for( int i =0; i < t; i++){ scanf("%d", &n); char cities[n]; scanf("%s", cities); vector<pair<int,int> > tmp; int good = 0; char city = '0'; bool prev_good = false; bool any_bad_before = false; for( int j=0; j < n; j++){ city = cities[j]; if (city == '0'){ good++; prev_good = true; if ( j == n-1 ){ if ( any_bad_before == false ){ tmp.push_back(make_pair(good, 0)); } else{ tmp.push_back(make_pair(good, 1)); } } continue; } else{ if (prev_good == true){ prev_good = false; if ( any_bad_before == false){ tmp.push_back(make_pair(good, 1)); good=0; any_bad_before = true; } else{ tmp.push_back(make_pair(good, 2)); good=0; } } else{ any_bad_before = true; continue; } } } if (tmp.size() == 1 && tmp[0].second == 0){ printf("%d\n", 0); continue; } sort(tmp.begin(), tmp.end(), [](auto a, auto b) { return float(a.first) / a.second > float(b.first) / b.second; }); int day = 0; long long int result = 0; for( auto g : tmp){ if ( g.first > day * g.second){ if (g.second == 0){ result += g.first; } else if(g.second == 1){ result += g.first - day; day++; } else{ result += 1; if (g.first - 2 *day > 2){ result += g.first - 2 *day -2; day++; } day++; } } else break; } printf("%d\n", n -result); } return 0; } |