Occupy the Cities
Time limit1sMemory limit1024 MB
Given a row of cities with some occupied, find the fewest rounds needed for every occupied city to take one adjacent empty city until all are occupied.
- Level
Medium7 of 10
- Topics
- String, Binary search, Greedy
- Solved
- No attempts yet
Problem
JB is playing a game. The game has cities, numbered . The -th and -th cities are adjacent if and only if or . Initially, some of the cities are occupied by JB.
The game runs in rounds. At the start of a round, each occupied city can mark at most one adjacent unoccupied city as its attack target. At the end of the round, all marked attack targets become occupied. The game ends when all the cities are occupied.
JB wants to occupy all the cities in the minimum number of rounds. Can you help him?
Input
There are multiple test cases. The first line contains a positive integer , the number of test cases. Each test case is as follows.
The first line contains an integer (), the number of cities.
The next line contains a string of length , consisting only of '0' and '1'. The -th character describes the initial state of the -th city: if is '1', the -th city is initially occupied by JB. Otherwise, it is not.
The sum of over all test cases does not exceed . At least one '1' is in .
Output
For each test case, output one line containing the minimum number of rounds needed to occupy all the cities.
Hint
In the second test case, the best way is .