#include<iostream> #include<vector> #include<algorithm> #include<list> using namespace std; #define MAXN 100000 void decrease_and_remove(list<int>* v, int decrement) { list<int>::iterator i = v->begin(); while (i != v->end()) { *i = *i - decrement; if (*i <= 0) i = v->erase(i); else ++i; } } void print_list(list<int>* v) { for (list<int>::iterator i = v->begin(); i != v->end(); ++i) cout << *i << " "; cout << endl; } void policz() { int n; vector<int> v; cin >> n; for (int i = 0; i < n; i++) { char val; cin >> val; if (val == '1') v.push_back(1); else v.push_back(0); } //for (int i = 0; i < n; ++i) { // cout << v[i]; //} int vaccined = 0; list<int> one_end; list<int> two_ends; int current_length = 0; int is_left_opened; if (v[0] == 0) is_left_opened = 1; else is_left_opened = 0; for (int i = 0; i < n; ++i) { if (v[i] == 0) current_length++; else { if (current_length > 0) { if (is_left_opened == 1) one_end.push_back(current_length); else two_ends.push_back(current_length); current_length = 0; is_left_opened = 0; } } } // All zeros if (current_length > 0 && is_left_opened) { cout << 0 << endl; return; } if (current_length > 0) one_end.push_back(current_length); //cout << "ONE END" << endl; //for (int i = 0; i < one_end.size(); ++i) cout << one_end[i]; //cout << endl; //cout << "TWO ENDS" << endl; //for (int i = 0; i < two_ends.size(); ++i) cout << two_ends[i]; //cout << endl; //cout << "=====================================" << endl; while (one_end.size() > 0 || two_ends.size() > 0) { //cout << "===================================================" << endl; //cout << "ITERATION " << endl; //cout << "ONE END "; print_list(&one_end); //cout << "TWO ENDS "; print_list(&two_ends); list<int>::iterator max_one; int max_one_value = 0; for (list<int>::iterator i = one_end.begin(); i != one_end.end(); ++i) { if (*i > max_one_value) { max_one_value = *i; max_one = i; }} list<int>::iterator max_two; int max_two_value = 0; for (list<int>::iterator i = two_ends.begin(); i != two_ends.end(); ++i) { if (*i > max_two_value) { max_two_value = *i; max_two = i; }} int variant = 0; if (max_one_value == 0) variant = 2; else if (max_two_value == 0) variant = 1; else if (max_two_value >= 2 * max_one_value + 1) variant = 2; else variant = 1; //cout << "MAX ONE " << max_one_value << " MAX TWO " << max_two_value << " VARIANT " << variant << endl; if (variant == 1) { //cout << "AAAAA 1" << endl; //list<int>::iterator max_one; //for (max_one = one_end.begin(); max_one != one_end.end(); ++max_one) { // if (*max_one == max_one_value) break; //} //cout << "AAAAA 2" << endl; vaccined += max_one_value; //cout << "AAAAA 3" << endl; one_end.erase(max_one); //cout << "MAX ONE END " << max_one_value << endl; } else { //cout << "BBBBB 1" << endl; //for (list<int>::iterator max_two = two_ends.begin(); max_two != two_ends.end(); ++max_two) { // if (*max_two == max_two_value) { two_ends.erase(max_two); break; }; //} //cout << "BBBBB 2" << endl; vaccined += 1; two_ends.erase(max_two); //cout << "BBBBB 3" << endl; //two_ends.erase(max_two); //cout << "BBBBB 4" << endl; one_end.push_back(max_two_value - 1); //cout << "MAX TWO ENDS " << max_two_value << endl; } //cout << "CCCCC 1" << endl; decrease_and_remove(&one_end, 1); decrease_and_remove(&two_ends, 2); //cout << "ONE END "; print_list(&one_end); //cout << "TWO ENDS "; print_list(&two_ends); //cout << "VACCINED " << vaccined << endl; } cout << (n - vaccined) << endl; } int main() { int t; cin >> t; for (int i = 0; i < t; ++i) policz(); }
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | #include<iostream> #include<vector> #include<algorithm> #include<list> using namespace std; #define MAXN 100000 void decrease_and_remove(list<int>* v, int decrement) { list<int>::iterator i = v->begin(); while (i != v->end()) { *i = *i - decrement; if (*i <= 0) i = v->erase(i); else ++i; } } void print_list(list<int>* v) { for (list<int>::iterator i = v->begin(); i != v->end(); ++i) cout << *i << " "; cout << endl; } void policz() { int n; vector<int> v; cin >> n; for (int i = 0; i < n; i++) { char val; cin >> val; if (val == '1') v.push_back(1); else v.push_back(0); } //for (int i = 0; i < n; ++i) { // cout << v[i]; //} int vaccined = 0; list<int> one_end; list<int> two_ends; int current_length = 0; int is_left_opened; if (v[0] == 0) is_left_opened = 1; else is_left_opened = 0; for (int i = 0; i < n; ++i) { if (v[i] == 0) current_length++; else { if (current_length > 0) { if (is_left_opened == 1) one_end.push_back(current_length); else two_ends.push_back(current_length); current_length = 0; is_left_opened = 0; } } } // All zeros if (current_length > 0 && is_left_opened) { cout << 0 << endl; return; } if (current_length > 0) one_end.push_back(current_length); //cout << "ONE END" << endl; //for (int i = 0; i < one_end.size(); ++i) cout << one_end[i]; //cout << endl; //cout << "TWO ENDS" << endl; //for (int i = 0; i < two_ends.size(); ++i) cout << two_ends[i]; //cout << endl; //cout << "=====================================" << endl; while (one_end.size() > 0 || two_ends.size() > 0) { //cout << "===================================================" << endl; //cout << "ITERATION " << endl; //cout << "ONE END "; print_list(&one_end); //cout << "TWO ENDS "; print_list(&two_ends); list<int>::iterator max_one; int max_one_value = 0; for (list<int>::iterator i = one_end.begin(); i != one_end.end(); ++i) { if (*i > max_one_value) { max_one_value = *i; max_one = i; }} list<int>::iterator max_two; int max_two_value = 0; for (list<int>::iterator i = two_ends.begin(); i != two_ends.end(); ++i) { if (*i > max_two_value) { max_two_value = *i; max_two = i; }} int variant = 0; if (max_one_value == 0) variant = 2; else if (max_two_value == 0) variant = 1; else if (max_two_value >= 2 * max_one_value + 1) variant = 2; else variant = 1; //cout << "MAX ONE " << max_one_value << " MAX TWO " << max_two_value << " VARIANT " << variant << endl; if (variant == 1) { //cout << "AAAAA 1" << endl; //list<int>::iterator max_one; //for (max_one = one_end.begin(); max_one != one_end.end(); ++max_one) { // if (*max_one == max_one_value) break; //} //cout << "AAAAA 2" << endl; vaccined += max_one_value; //cout << "AAAAA 3" << endl; one_end.erase(max_one); //cout << "MAX ONE END " << max_one_value << endl; } else { //cout << "BBBBB 1" << endl; //for (list<int>::iterator max_two = two_ends.begin(); max_two != two_ends.end(); ++max_two) { // if (*max_two == max_two_value) { two_ends.erase(max_two); break; }; //} //cout << "BBBBB 2" << endl; vaccined += 1; two_ends.erase(max_two); //cout << "BBBBB 3" << endl; //two_ends.erase(max_two); //cout << "BBBBB 4" << endl; one_end.push_back(max_two_value - 1); //cout << "MAX TWO ENDS " << max_two_value << endl; } //cout << "CCCCC 1" << endl; decrease_and_remove(&one_end, 1); decrease_and_remove(&two_ends, 2); //cout << "ONE END "; print_list(&one_end); //cout << "TWO ENDS "; print_list(&two_ends); //cout << "VACCINED " << vaccined << endl; } cout << (n - vaccined) << endl; } int main() { int t; cin >> t; for (int i = 0; i < t; ++i) policz(); } |