#K63462. House Repair Scheduling
House Repair Scheduling
House Repair Scheduling
You are given n houses in a row (numbered from 1 to n) and an integer d representing the number of days available for repairs. Each house has an initial state given by a string of length n consisting only of the characters 'S'
, 'D'
, and 'R'
.
The meaning of each state is as follows:
S
: The house is safe and does not need any repairs.D
: The house is damaged and requires exactly 1 repair.R
: The house is ruined and requires exactly 2 repairs. Moreover, if a house is ruined, its two repairs must be performed on two different days, with the first repair occurring before the second.
Your task is to decide whether it is possible to perform repairs on all houses that require repair and complete the repair work in exactly d days. On each day, you can repair exactly 1 house. If a house is ruined, its repair actions must occur on two separate days. If it is possible, output any valid repair schedule. Otherwise, output -1
.
Note: The repair schedule should consist of exactly d lines; on each day, print the 1-based index of the house being repaired.
A simple strategy is as follows: Let the number of ruined houses be R and the number of damaged houses be D. The total number of repair actions required is 2R + D. The schedule is possible if and only if 2R + D = d. One valid schedule (if possible) is to first perform the first repair for each ruined house (in order of appearance), then repair all damaged houses, and finally perform the second repair for each ruined house (in the same order). This guarantees that for each ruined house the first repair is done before the second.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains two integers n and d separated by space.
- The second line contains a string of length n which is the state of the houses (each character is either 'S', 'D', or 'R').
outputFormat
If a valid repair schedule exists, output d lines to stdout, each line containing a single integer representing the 1-based index of the house repaired on that day.
If it is impossible to complete the repairs in exactly d days, output -1
.
5 5
SSRDR
3
5
4
3
5
</p>