House Deconstruction
Time limit1sMemory limit2048 MB
Given people and houses on a circle, find the minimum total walking distance after destroying some houses, and count the sets of destroyed houses that reach it.
- Level
Hard9 of 10
- Topics
- Combinatorics, Greedy, Math
- Solved
- No attempts yet
Problem
In the land of Circleland, there is a circle with equally spaced points around its circumference. The distance between any two adjacent points is .
Each point holds a person, an empty house, or nothing. Each person wants to walk to a different house, and each house can contain at most one person. People can only walk along the circumference of the circle and cannot cut across.
There are more houses than people, so you want to destroy some of the houses. Suppose you destroy a set of houses . Let be the minimum total walking distance needed to move each person to a different house that was not destroyed.
Compute the minimum value of . Then compute how many sets achieve this minimum. Since that count can be large, output it modulo .
Input
The first line contains three integers , , and (), where is the number of points around the circle, is the number of people, and is the number of houses. The points are labeled to , and point is adjacent to point .
The next lines each contain an integer () and a character (P,H). Here is the position of a person or house, and is the type of the point: P for person, H for house. All positions are distinct and given in increasing order.
Output
Output two lines. The first line contains the minimum possible value of . The second line contains the number of sets that achieve this minimum, modulo .
Hint
In the first sample, the minimum total walking distance is . Destroying the houses at positions and , and , or and achieves it.
In the second sample, destroying the houses at positions and gives a minimum total walking distance of . The minimum can be reached in more than one way, but the set of destroyed houses is the same, so it is counted once.