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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// 2021-2-pan-pandemia.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>


constexpr int MAXN = 1e5 + 7;

int srt_hm[MAXN];

// O(length+n)
void srt_custom(int* b, int* e, int n)
{
	for (size_t i = 0; i <= n; i++)
	{
		srt_hm[i] = 0;
	}
	for (int* i = b; i < e; i++)
	{
		srt_hm[*i]++;
	}
	int cnt = 0;
	for (int i = 0; i <= n;)
	{
		if (srt_hm[i])
		{
			b[cnt] = i;
			cnt++;
			srt_hm[i]--;
		}
		else
		{
			i++;
		}
	}
}


// -1, -2
int t1[MAXN]; // stack
int t12[MAXN]; // queue
int t2[MAXN]; // stack
int tm;
int answ;
int l1, l2, l12b, l12e;


inline void mv2()
{
	//std::cout << "mv2 ";
	l2--;
	if (t2[l2] > 2 * tm)
	{
		answ++;
		t12[l12e] = t2[l2] - 1 - tm;
		l12e++;
	}
}

inline void mv1()
{
	//std::cout << "mv1 ";
	if (l12b==l12e) // empty queue
	{
		l1--;
		answ += std::max(t1[l1] - tm, 0);
		return;
	}
	if (l1 == 0) // empty stack
	{
		answ += std::max(t12[l12b] - tm, 0);
		l12b++;
		return;
	}
	if (t12[l12b] > t1[l1 - 1])
	{
		answ += std::max(t12[l12b] - tm, 0);
		l12b++;
		return;
	}
	else
	{
		l1--;
		answ += std::max(t1[l1] - tm, 0);
		return;
	}
}

inline bool empty1()
{
	return l1 == 0 && l12b == l12e;
}

inline bool empty2()
{
	return l2 == 0;
}

inline int mx2()
{
	return t2[l2 - 1] - 2 * tm;
}

inline int mx1()
{
	if (l12b == l12e) // empty queue
	{
		return t1[l1 - 1] - tm;
	}
	if (l1 == 0) // empty stack
	{
		return t12[l12b] - tm;
	}
	if (t12[l12b] > t1[l1 - 1])
	{
		return t12[l12b] - tm;
	}
	else
	{
		return t1[l1 - 1] - tm;
	}
}

inline void handle_stuff()
{
	answ = 0;
	tm = 0;
	while (!empty1() || !empty2())
	{
		//std::cout << answ << ' ';
		if (empty1())
		{
			mv2();
			tm++;
			continue;
		}
		if (empty2())
		{
			mv1();
			tm++;
			continue;
		}
		if (mx2() > 2 * mx1())
		{
			mv2();
			tm++;
			continue;
		}
		else
		{
			mv1();
			tm++;
			continue;
		}
	}
}
/*
void print_vars(int n)
{
	std::cout << tm << ' ' << answ << ' ' << l1 << ' ' << l2 << ' ' << l12b << ' ' << l12e << '\n';
	for (size_t i = 0; i < l1; i++)
	{
		std::cout << t1[i] << ' ';
	}
	std::cout << '\n';
	for (size_t i = 0; i < l2; i++)
	{
		std::cout << t2[i] << ' ';
	}
	std::cout << '\n';
	// clearstuff

	for (size_t i = l1; i <= n+10; i++)
	{
		t1[i] = 0;
	}
	for (size_t i = l2; i <= n+10; i++)
	{
		t2[l2] = 0;
	}
	for (size_t i = 0; i < n+11; i++)
	{
		t12[i] = 0;
	}
}*/

int main()
{
	std::ios_base::sync_with_stdio(0);
	std::cin.tie(0);
	std::cout.tie(0);
	int qq, n, cnt;
	bool isb;
	std::string s;
	std::cin >> qq;
	while (qq--)
	{
		std::cin >> n;
		std::cin >> s;
		cnt = 0;
		isb = true;
		l1 = 0;
		l2 = 0;
		tm = 0;
		answ = 0;
		l12b = 0;
		l12e = 0;
		for (size_t i = 0; i < n; i++)
		{
			if (s[i] == '0')
			{
				cnt++;
			}
			else
			{
				if (cnt == 0)
				{
					isb = false;
					continue;
				}
				if (isb)
				{
					t1[l1] = cnt;
					l1++;
					isb = false;
				}
				else
				{
					t2[l2] = cnt;
					l2++;
				}

				cnt = 0;
			}
		}
		if (isb)
		{
			std::cout << 0 << '\n';
			continue;
		}
		if (cnt)
		{
			t1[l1] = cnt;
			l1++;
		}
		srt_custom(t1, t1 + l1, n);
		srt_custom(t2, t2 + l2, n);
		//print_vars(n);

		handle_stuff();
		std::cout << n - answ << '\n';
		/*
		std::cout << "t1:\n";
		for (size_t i = 0; i < l1; i++)
		{
			std::cout << t1[i] << ' ';
		}
		std::cout << "\nt2:\n";
		for (size_t i = 0; i < l2; i++)
		{
			std::cout << t2[i] << ' ';
		}
		std::cout << '\n';*/
	}
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file