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
#include <vector>
#include <iostream>
#include <array>
#include <cinttypes>
#include <cstring>
#include <climits>
#include <string>
#include <algorithm>
using namespace std;
namespace wzor
{
}
int solve_wzor(int n, char* input)
{
	using namespace wzor;
	vector<pair<int, int>> intervals;
	// Find first 1
	int first_one = -1;
	int prev_one = 0;
	input -= 1;
	for(int i = 1; i <= n; ++i)
	{
		if(input[i] == '1')
		{
			if(prev_one == 0)
			{
				intervals.push_back({i - 1, 0});
			}
			else
			{
				int length = i - prev_one - 1;
				intervals.push_back({length / 2, length - length / 2});
			}
			prev_one = i;
		}
	}
	intervals.push_back({n - prev_one, 0});
	std::sort(intervals.rbegin(), intervals.rend());
	int step_counter = 0;
	int saves = 0;
	auto try_step = [&](int step_size)
	{
		if(step_size - step_counter > 0)
		{
			saves += step_size - step_counter;
			++step_counter;
			//cout << "Trying step: " << step_size << ' ' << step_counter << ' ' << saves << '\n';
		}
	};
	for(auto [p1, p2] : intervals)
	{
		//cout << "P1/P2: " << p1 << "/" << p2 << "\n";
		try_step(p2);
		try_step(p1);
	}
	return n - saves;
}
#include <iostream>

char input[100100];
int main()
{
	int t;
	std::cin >> t;
	while(t--)
	{
		int n;
		std::cin >> n >> input;
		std::cout << solve_wzor(n, input) << '\n';
	}
}