Stamp
SRM 558 · 2012-06-05 · by ir5
Problem Statement
You are given a
You are also given the
In the second phase, Jiro must repeatedly use the stamp to color the cells. Each use of the stamp works as follows:
- Jiro picks one of the three colors and pushes the stamp into ink of the chosen color C.
- Jiro picks a segment of L contiguous cells such that each of them is either uncolored or already has the color C. The segment must be completely inside the board. That is, the leftmost cell of the segment must be one of the cells 0 through N-L.
- Jiro pushes the stamp onto the chosen segment of cells. All the cells now have color C.
Return the smallest possible total cost of coloring all the cells using the above process.
Constraints
- desiredColor will contain between 1 and 50 characters, inclusive.
- Each character of desiredColor will be either 'R' or 'G' or 'B' or '*'.
- stampCost will be between 1 and 100,000, inclusive.
- pushCost will be between 1 and 100,000, inclusive.
"RRGGBB" 1 1 Returns: 5
The optimal solution is to choose L=2 and then stamp three times: using red color for cells [0,1], green for [2,3], and blue for [4,5]. The stamp costs 2*1 = 2, each of the three uses costs 1, so the total costs is 2*1 + 3*1 = 5.
"R**GB*" 1 1 Returns: 5
The optimal solution is the same as in the previous example. Note that you must color all the cells, so choosing L=1 and then using the stamp three times is not a valid solution.
"BRRB" 2 7 Returns: 30
Also, note that once a cell is colored, you are not allowed to stamp over it using a different color. Therefore, you can only choose L=1 in this case.
"R*RR*GG" 10 58 Returns: 204
It is allowed to stamp the same cell multiple times if all of those stamps use the same color.
"*B**B**B*BB*G*BBB**B**B*" 5 2 Returns: 33
Submissions are judged against all 146 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class Stamp with a public method int getMinimumCost(string desiredColor, int stampCost, int pushCost) · 146 test cases · 2 s / 256 MB per case