#include <iostream> #include <vector> #include <algorithm> #include <array> //#include <map> #include <math.h> #include <map> // #include <bits/stdc++.h> using namespace std; long zarazonych; string ch; struct greater2 { template<class T> bool operator()(T const &a, T const &b) const { return a > b; } }; void print_v(vector<array<long, 3> > vect) { for (array<long, 3> i : vect) { for (auto x : i) cout << x << " "; cout << endl; } } long ile_zarazonych(string miasta) { long a = 0; long size = miasta.size(); vector<array<long, 3> > niezarazone; zarazonych = 0; long max = 0; long dl = 0; for (long i = 1; i < size; ++i) { if (miasta[i] == '1') zarazonych++; if (miasta[i] == '1' && miasta[i - 1] == '0' ) { //koniec ciagu wolnych miast // cout << "a: " << a << " dl: " << i - a << "\n"; // int c; dl = i - a; if (a == 0) { // poczatkowe niezarazone.push_back({dl * 2, dl, -1}); } else { // if (max < dl) { // max = dl; // } niezarazone.push_back({dl, dl, -2}); } } else if (miasta[i] == '0' && miasta[i - 1] == '1' ) { a = i; } } if (miasta[size - 1] == '0') { // cout << "a2: " << a << " dl size: " << size - a << "\n"; dl = size - a; niezarazone.push_back({dl * 2, dl, -1}); } sort(niezarazone.begin(), niezarazone.end(), greater2()); // print_v(niezarazone); // cout << "zarazonych: " << zarazonych << "\n"; long kum = 0; long miasta_ocalone = 0; long ogniska; long suma_ocalonych = 0; for (long i = 0; i < niezarazone.size(); ++i) { // miasta_ocalone = niezarazone[i][0] - kum; ogniska = -niezarazone[i][2]; long m = niezarazone[i][1]; if (ogniska == 1) { miasta_ocalone = m - kum; if (miasta_ocalone > 0) { kum += 1; } } else if (ogniska == 2) { miasta_ocalone = m - kum * 2; if (miasta_ocalone > 1) { miasta_ocalone -= 1; } if (miasta_ocalone > 1) { kum += 2; } else if (miasta_ocalone > 0) { kum += 1; } } if (miasta_ocalone > 0) suma_ocalonych += miasta_ocalone; // cout << "i: " << i << " m:" << m << " miasta_ocalone: " << miasta_ocalone << " ogniska: " << ogniska << " kum out: " << kum << " suma_ocalonych: " << suma_ocalonych << "\n"; } // cout << "suma_ocalonych: " << suma_ocalonych << " size: " << size << "\n"; return size - suma_ocalonych; } int main() { long t; cin >> t; long n; string miasta; long wynik; for (long i = 0; i < t; i++) { cin >> n; cin >> miasta; // cout << "\nn: " << n << " miasta: " << miasta << "\n"; wynik = ile_zarazonych(miasta); // cout << "WWWWW --- zarazonych wynik: " << wynik << "\n"; cout << wynik << "\n"; } //cout << "sprawdzam wynik:" << suma_slowa(wynik) << "\n"; }
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 | #include <iostream> #include <vector> #include <algorithm> #include <array> //#include <map> #include <math.h> #include <map> // #include <bits/stdc++.h> using namespace std; long zarazonych; string ch; struct greater2 { template<class T> bool operator()(T const &a, T const &b) const { return a > b; } }; void print_v(vector<array<long, 3> > vect) { for (array<long, 3> i : vect) { for (auto x : i) cout << x << " "; cout << endl; } } long ile_zarazonych(string miasta) { long a = 0; long size = miasta.size(); vector<array<long, 3> > niezarazone; zarazonych = 0; long max = 0; long dl = 0; for (long i = 1; i < size; ++i) { if (miasta[i] == '1') zarazonych++; if (miasta[i] == '1' && miasta[i - 1] == '0' ) { //koniec ciagu wolnych miast // cout << "a: " << a << " dl: " << i - a << "\n"; // int c; dl = i - a; if (a == 0) { // poczatkowe niezarazone.push_back({dl * 2, dl, -1}); } else { // if (max < dl) { // max = dl; // } niezarazone.push_back({dl, dl, -2}); } } else if (miasta[i] == '0' && miasta[i - 1] == '1' ) { a = i; } } if (miasta[size - 1] == '0') { // cout << "a2: " << a << " dl size: " << size - a << "\n"; dl = size - a; niezarazone.push_back({dl * 2, dl, -1}); } sort(niezarazone.begin(), niezarazone.end(), greater2()); // print_v(niezarazone); // cout << "zarazonych: " << zarazonych << "\n"; long kum = 0; long miasta_ocalone = 0; long ogniska; long suma_ocalonych = 0; for (long i = 0; i < niezarazone.size(); ++i) { // miasta_ocalone = niezarazone[i][0] - kum; ogniska = -niezarazone[i][2]; long m = niezarazone[i][1]; if (ogniska == 1) { miasta_ocalone = m - kum; if (miasta_ocalone > 0) { kum += 1; } } else if (ogniska == 2) { miasta_ocalone = m - kum * 2; if (miasta_ocalone > 1) { miasta_ocalone -= 1; } if (miasta_ocalone > 1) { kum += 2; } else if (miasta_ocalone > 0) { kum += 1; } } if (miasta_ocalone > 0) suma_ocalonych += miasta_ocalone; // cout << "i: " << i << " m:" << m << " miasta_ocalone: " << miasta_ocalone << " ogniska: " << ogniska << " kum out: " << kum << " suma_ocalonych: " << suma_ocalonych << "\n"; } // cout << "suma_ocalonych: " << suma_ocalonych << " size: " << size << "\n"; return size - suma_ocalonych; } int main() { long t; cin >> t; long n; string miasta; long wynik; for (long i = 0; i < t; i++) { cin >> n; cin >> miasta; // cout << "\nn: " << n << " miasta: " << miasta << "\n"; wynik = ile_zarazonych(miasta); // cout << "WWWWW --- zarazonych wynik: " << wynik << "\n"; cout << wynik << "\n"; } //cout << "sprawdzam wynik:" << suma_slowa(wynik) << "\n"; } |