#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
bool malejaco(int a, int b) {
return a > b;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int t, n, pocz, kon, ost = -1, id, wynik;
string miasta;
vector<int> dystanse;
cin >> t;
for(int i=0; i<t; i++) {
ost = -1;
cin >> n;
cin >> miasta;
dystanse.clear();
for(int j=0; j<n; j++) {
if(miasta[j] == '1') {
if(ost != -1) {
dystanse.push_back(j-ost-1);
} else {
pocz = j;
}
ost = j;
}
}
kon = n-ost-1;
sort(dystanse.begin(), dystanse.end(), malejaco);
id = 0;
wynik = 0;
for(int j=0; j<dystanse.size(); j++) {
if(pocz-id >= dystanse[j]-(2*id)-1 && pocz-id > 0) {
wynik += pocz-id;
pocz = 0;
id++;
}
if(kon-id >= dystanse[j]-(2*id)-1 && kon-id > 0) {
wynik += kon-id;
kon = 0;
id++;
}
if(dystanse[j]-(2*id)-1 > 0) {
wynik += dystanse[j]-(2*id)-1;
id += 2;
} else if(dystanse[j]-(2*id) == 1) {
wynik += 1;
id += 1;
}
}
if(pocz != 0) {
if(pocz-id > 0) {
wynik += pocz-id;
pocz = 0;
id++;
}
}
if(kon != 0) {
if(kon-id > 0) {
wynik += kon-id;
kon = 0;
id++;
}
}
cout << n-wynik << "\n";
}
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 | #include <iostream> #include <algorithm> #include <cmath> #include <vector> using namespace std; bool malejaco(int a, int b) { return a > b; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int t, n, pocz, kon, ost = -1, id, wynik; string miasta; vector<int> dystanse; cin >> t; for(int i=0; i<t; i++) { ost = -1; cin >> n; cin >> miasta; dystanse.clear(); for(int j=0; j<n; j++) { if(miasta[j] == '1') { if(ost != -1) { dystanse.push_back(j-ost-1); } else { pocz = j; } ost = j; } } kon = n-ost-1; sort(dystanse.begin(), dystanse.end(), malejaco); id = 0; wynik = 0; for(int j=0; j<dystanse.size(); j++) { if(pocz-id >= dystanse[j]-(2*id)-1 && pocz-id > 0) { wynik += pocz-id; pocz = 0; id++; } if(kon-id >= dystanse[j]-(2*id)-1 && kon-id > 0) { wynik += kon-id; kon = 0; id++; } if(dystanse[j]-(2*id)-1 > 0) { wynik += dystanse[j]-(2*id)-1; id += 2; } else if(dystanse[j]-(2*id) == 1) { wynik += 1; id += 1; } } if(pocz != 0) { if(pocz-id > 0) { wynik += pocz-id; pocz = 0; id++; } } if(kon != 0) { if(kon-id > 0) { wynik += kon-id; kon = 0; id++; } } cout << n-wynik << "\n"; } return 0; } |
English