There are n houses in a row numbered 1,2,⋯,𝑛 . The 𝑖 -th house and the 𝑗 -th house…
- User
There are n houses in a row numbered 1,2,⋯,𝑛 . The 𝑖 -th house and the 𝑗 -th house are adjacent if |𝑖−𝑗|=1 . Initially, some of the houses have already been robbed by Bob's friends, with a robber stationed in each of these robbed houses.
Bob wants to ensure that all the remaining unrobbed houses are robbed in the minimum number of rounds. In each round, every robber can rob at most one unrobbed house that is adjacent to the house he is currently stationed at. After robbing a house, the robber returns back to his original house. Once a house is robbed, a new robber is placed there, allowing it to participate in future rounds. The game ends when all the houses have been robbed.
Bob’s goal is to rob all the houses in the minimum number of rounds.
Input Each test contains multiple test cases. The first line contains the number of test cases 𝑡(1≤𝑡≤104) . The description of the test cases follows.
The first line contains an integer 𝑛(1≤𝑛≤106) , indicating the number of houses.
The next line contains a binary string 𝑠 of length 𝑛 . The 𝑖 -th character describes the initial state of the 𝑖 -th house: if 𝑠𝑖= '1', the 𝑖 -th house is already robbed by Bob. If 𝑠𝑖= '0', the 𝑖 -th house is not robbed by Bob.
It's guaranteed that the sum of 𝑛 over all the test cases doesn't exceed 106 . It's also guaranteed that there is at least one '1' in 𝑠 .
Output For each test case, output one line containing the minimum number of rounds to rob all the houses.
Input cases: 4 6 101010 5 00100 4 1111 6 001000
output:
- Assistant
To solve this problem, we need to find the minimal number of rounds required to rob all the unrobbed houses following the given rules. The key idea is to observe that each house can be robbed at the earliest rounds equal to the shortest distance from any initially robbed house (a house with '1' in the input string).
Here's a step-by-step guide to solve the problem:
Algorithm:
- For each position in the string, calculate the minimum distance to the nearest '1' (robbed house).
- The maximum of these minimum distances will be the minimal number of rounds needed since the farthest unrobbed house determines the rounds required.
- Output the maximum distance for each test case.
Example Cases Explained: