-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB_Seats.cpp
More file actions
59 lines (47 loc) · 1.12 KB
/
B_Seats.cpp
File metadata and controls
59 lines (47 loc) · 1.12 KB
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
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define input for(int &i:v) cin >> i;
void print(const vector<int>& v) {
for (int i : v) {
cout << i << " ";
}
cout << endl;
}
void solve() {
int n;
cin >> n;
string s;
cin >> s;
int existing = 0;
for(char c : s) if(c == '1') existing++;
int add = 0;
for(int i = 0; i < n; i++) {
if(s[i] != '0') continue;
int j = i;
while(j < n && s[j] == '0') j++;
int len = j - i;
bool left = (i > 0 && s[i-1] == '1');
bool right = (j < n && s[j] == '1');
if(!left && !right) {
add += (len + 2) / 3;
}
else if(left && right) {
add += max(0LL, (len / 3));
}
else {
add += (len + 1) / 3;
}
i = j - 1;
}
cout << existing + add << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--) solve();
}