#include <iostream> #include <algorithm> #ifdef TEST_MODE #include <fstream> #endif using namespace std; const int kMaxCities = 100000; int main() { #ifdef TEST_MODE std::ifstream in("140.in"); std::cin.rdbuf(in.rdbuf()); #endif std::ios_base::sync_with_stdio(false); int hRanges[kMaxCities+1]; int t; cin >> t; while ( t-- ) { int n; int noHealthyCities = 0; int days = 0; char cities[kMaxCities+1]; int leftRange, rightRange; leftRange = rightRange = -1; cin >> n; for ( int i = 0; i < n; ++i ) { hRanges[i] = 0; } cin >> cities; int r = 0; for ( int i = 0; i < n; ++i ) { if ( cities[i] == '0' ){ r++; } else { if ( leftRange < 0 ) { leftRange = r; } else if ( r > 0 ){ hRanges[r]++; } r = 0; } } rightRange = r; if ( n == 1 && rightRange == 1) { noHealthyCities = 1; } for ( int i = n - 1; i > days * 2; --i ) { if ( i - days * 2 <= (leftRange - days) * 2 ) { noHealthyCities += leftRange - days; leftRange = 0; days++; } if ( i - days * 2 <= (rightRange - days) * 2 ) { noHealthyCities += rightRange - days; rightRange = 0; days++; } if ( hRanges[i] > 0 ) { int td = (i - days * 2 + 1) / 4; int d = min(td, hRanges[i]); noHealthyCities += ((i - days * 2 - 1) + (i - days * 2 - (d - 1) * 4 - 1)) * d / 2; if ( td < hRanges[i] && ((i - days * 2) % 4 == 1 || (i - days * 2) % 4 == 2)) { noHealthyCities++; days++; } days += d * 2; } } cout << n - noHealthyCities << 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 75 76 77 78 | #include <iostream> #include <algorithm> #ifdef TEST_MODE #include <fstream> #endif using namespace std; const int kMaxCities = 100000; int main() { #ifdef TEST_MODE std::ifstream in("140.in"); std::cin.rdbuf(in.rdbuf()); #endif std::ios_base::sync_with_stdio(false); int hRanges[kMaxCities+1]; int t; cin >> t; while ( t-- ) { int n; int noHealthyCities = 0; int days = 0; char cities[kMaxCities+1]; int leftRange, rightRange; leftRange = rightRange = -1; cin >> n; for ( int i = 0; i < n; ++i ) { hRanges[i] = 0; } cin >> cities; int r = 0; for ( int i = 0; i < n; ++i ) { if ( cities[i] == '0' ){ r++; } else { if ( leftRange < 0 ) { leftRange = r; } else if ( r > 0 ){ hRanges[r]++; } r = 0; } } rightRange = r; if ( n == 1 && rightRange == 1) { noHealthyCities = 1; } for ( int i = n - 1; i > days * 2; --i ) { if ( i - days * 2 <= (leftRange - days) * 2 ) { noHealthyCities += leftRange - days; leftRange = 0; days++; } if ( i - days * 2 <= (rightRange - days) * 2 ) { noHealthyCities += rightRange - days; rightRange = 0; days++; } if ( hRanges[i] > 0 ) { int td = (i - days * 2 + 1) / 4; int d = min(td, hRanges[i]); noHealthyCities += ((i - days * 2 - 1) + (i - days * 2 - (d - 1) * 4 - 1)) * d / 2; if ( td < hRanges[i] && ((i - days * 2) % 4 == 1 || (i - days * 2) % 4 == 2)) { noHealthyCities++; days++; } days += d * 2; } } cout << n - noHealthyCities << endl; } return 0; } |