#P8898. [USACO22DEC] Feeding the Cows B
[USACO22DEC] Feeding the Cows B
Description
Farmer John has cows, each of which is either Guernsey or Holstein. They stand in a row along a horizontal line, occupying positions .
Since the cows are hungry, FJ decides to plant grass patches at some positions among . Guernseys and Holsteins prefer different types of grass, so if Farmer John decides to plant grass at a position, he must choose either the type preferred by Guernseys or the type preferred by Holsteins—he cannot plant both types at the same position. Each planted grass patch can feed an unlimited number of cows of the corresponding breed.
Each cow is willing to move at most positions to reach a grass patch. Determine the minimum number of grass patches needed to feed all cows. In addition, output one planting plan that feeds all cows using the minimum number of patches. Any plan that satisfies the conditions is considered correct.
Input Format
Each input contains test cases, each describing an arrangement of cows. The first line contains . Then test cases follow.
For each test case, the first line contains and . The second line contains a string of length , where the -th character indicates the breed of the -th cow ( for Guernsey, for Holstein).
Output Format
For each of the test cases, output two lines. The first line should contain the minimum number of grass patches needed to feed all cows. The second line should contain a string of length representing one optimal planting plan. The -th character corresponds to position : output if no grass is planted, if grass for Guernseys is planted, and if grass for Holsteins is planted. Any valid plan will be accepted.
6
5 0
GHHGG
5 1
GHHGG
5 2
GHHGG
5 3
GHHGG
5 4
GHHGG
2 1
GH
5
GHHGG
3
.GH.G
2
..GH.
2
...GH
2
...HG
2
HG
Hint
Explanation for Sample 1
Note that for some test cases, there are multiple optimal planting plans using the minimum number of patches. For example, in the fourth test case, the following is another acceptable answer:
This plan plants a Guernsey-feeding patch at position and a Holstein-feeding patch at position . This uses the minimum number of patches and ensures that all cows are within positions of their preferred grass.
Testdata Properties
- Testdata – satisfies .
- Testdata – satisfies .
- Testdata – satisfies .
Translated by ChatGPT 5
京公网安备 11011102002149号