Trash Bins
InterviewTime limit20sMemory limit1024 MB
Given a string of 0s and 1s marking houses with trash bins, sum the distance from every house to its nearest bin.
- Level
Medium4 of 10
- Topics
- Array, Implementation, Greedy, String
- Solved
- No attempts yet
Problem
In the city where you live, Kickstartland, there is one particularly long street with houses on it. This street has length , and the houses are evenly placed along it, that is, the first house is at position , the second house is at position , and so on. The distance between any pair of houses and is , where denotes the absolute value of .
Some of these houses have trash bins in front of them. That means that the owners of such houses do not have to walk when they want to take their trash out. However, for the owners of houses that do not have trash bins in front of them, they have to walk towards some house that has a trash bin in front of it, either to the left or to the right of their own house.
To save time, every house owner always takes their trash out to the trash bin that is closest to their houses. If there are two trash bins that are both the closest to a house, then the house owner can walk to any of them.
Given the number of houses , and the description of which of these houses have trash bins in front of them, find out what is the sum of the distances that each house owner has to walk to take their trashes out. You can assume that at least one house has a trash bin in front of it.
Input
The first line of the input gives the number of test cases, . test cases follow. Each test case consists of two lines.
The first line of each test case contains an integer , denoting the number of houses on the street.
The second line of each test case contains a string of length , representing which houses have trash bins in front of them. If the -th character in string is equal to , then it means that the -th house has a trash bin in front of it. Otherwise, if it is equal to , then it means that the -th house does not have a trash bin in front of it.
Output
For each test case, output one line containing Case #x: y, where is the test case number (starting from 1) and is the sum of the distances that each house owner has to walk to take their trashes out.
Limits
- .
- The length of is equal to .
- Each character of is either or .
- There is at least one character in .
Hint
For the first test case, every house has a trash bin in front of it, and therefore none of the house owners will have to walk to take their trashes out.
For the second test case, the first and the fourth house owners have trash bins in front of their houses, and therefore will not have to walk. The second house owner will walk towards the first house, and the distance will be equal to . The third, fifth, and sixth house owners will walk towards the fourth house, and the distances will be equal to , , and , respectively.