#include<bits/stdc++.h>
using namespace std;
int t;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> t;
for (int xyz = 1; xyz <= t; xyz++)
{
int n;
string s;
cin >> n >> s;
vector <int> v;
int num1 = 0, last1 = 0;
for (int i = 0; i < n; i++)
{
if (s[i] == '0')
continue;
if (num1 == 0)
v.push_back(i);
else{
v.push_back((i - last1) / 2);
v.push_back((i - last1 - 1) / 2);
}
num1++;
last1 = i;
}
if (last1 != 0)
v.push_back(n - last1 - 1);
if (num1 == 0){
cout << "0\n";
continue;
}
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
int res = 0;
for (int i = 0; i < v.size(); i++){
res += max(0, v[i] - i);
}
cout << (n - res) << "\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 | #include<bits/stdc++.h> using namespace std; int t; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; for (int xyz = 1; xyz <= t; xyz++) { int n; string s; cin >> n >> s; vector <int> v; int num1 = 0, last1 = 0; for (int i = 0; i < n; i++) { if (s[i] == '0') continue; if (num1 == 0) v.push_back(i); else{ v.push_back((i - last1) / 2); v.push_back((i - last1 - 1) / 2); } num1++; last1 = i; } if (last1 != 0) v.push_back(n - last1 - 1); if (num1 == 0){ cout << "0\n"; continue; } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); int res = 0; for (int i = 0; i < v.size(); i++){ res += max(0, v[i] - i); } cout << (n - res) << "\n"; } return 0; } |
English