#P14369. [ICPC 2024 NAC] House Deconstruction
[ICPC 2024 NAC] House Deconstruction
Problem Description
In Circleland, there is a circle with x equally spaced points on its circumference.
Adjacent points are at distance 1 along the circle.
Some points contain people, some contain houses, and some contain nothing.
Each person wants to walk to a different house, and each house can hold at most one person.
People may only walk along the circumference of the circle.
There are more houses than people, so you will destroy some houses.
If you destroy a set of houses S, let f(S) be the minimum total walking distance needed to send every person to a different house that is not destroyed.
Your task is:
- compute the minimum possible value of
f(S), - count how many sets
Sachieve that minimum.
Because the number of valid sets can be large, output it modulo 998244353.
Input
The first line contains three integers x, n, and m:
1 <= n < m <= 2 * 10^5n + m <= x <= 10^9
where:
xis the number of points on the circle,nis the number of people,mis the number of houses.
Points are labeled from 1 to x, and point x is adjacent to point 1.
The next n + m lines each contain:
p t
where:
1 <= p <= xtis eitherPorH
p is the position of the point, and t indicates whether that point contains a person (P) or a house (H).
All positions are distinct and are given in increasing order.
Output
Output two lines:
- the minimum possible value of
f(S), - the number of sets
Sthat achieve this minimum, modulo998244353.
Sample Explanation
For the first sample, the minimum total walking distance is 2.
Possible destroyed-house sets are {2, 5}, {4, 5}, and {5, 6}.
For the second sample, the minimum total walking distance is 4.
Destroying {6, 31415926} achieves this value.
Even if the minimum matching can be realized in multiple ways, it is counted only once because the destroyed-house set is the same.
Sample Input 1
6 2 4
1 P
2 H
3 P
4 H
5 H
6 H
Sample Output 1
2
3
Sample Input 2
1000000000 2 4
1 P
6 H
31415926 H
999999998 H
999999999 H
1000000000 P
Sample Output 2
4
1