TopCoder problem "ScoreRecomposition" used in SRM 309 (Division I Level One , Division II Level Two)



Problem Statement

    

You took a test consisting of N questions, each of which has a distinct point value between 1 and N, inclusive, and you have finally received the results. Along with your final score, you are told which questions you answered correctly. However, you are not given the point values that were assigned to the questions. For each correct answer, you received the full point value of the question, and for each wrong answer, you received 0 points. You must determine the minimum possible error of a valid point assignment that would result in the final score that you received. The error of a valid assignment of points is defined as follows: For each question i (where i is a 1-based index), let e(i) = the absolute value of (i minus the point value of the question). The error of the point assignment is the maximum value of e(i).

Given a String questions and an int score, return an int representing the minimum possible error of a valid point assignment. The ith character (where i is a 1-based index) of questions is either 'C', meaning that you answered question i correctly, or 'W', meaning that you answered it wrong. If there is no valid point assignment, return -1.

 

Definition

    
Class:ScoreRecomposition
Method:minError
Parameters:String, int
Returns:int
Method signature:int minError(String questions, int score)
(be sure your method is public)
    
 

Constraints

-questions will contain exactly N elements, where N is between 1 and 10, inclusive.
-Each character of questions will be either 'C' or 'W'.
-score will be between 0 and N*(N+1)/2, inclusive.
 

Examples

0)
    
"CCC"
5
Returns: -1
Contact the contest director immediately! Since you answered every question correctly, your score should be 6.
1)
    
"WCWW"
4
Returns: 2
Obviously, you answered only the 4-point question correctly.
2)
    
"CWW"
1
Returns: 0
The minimum error occurs when each question i is assigned a point value of i.
3)
    
"CWCC"
6
Returns: 2
One valid point assignment with the lowest possible error is 1, 4, 2, 3.
4)
    
"WWCC"
3
Returns: 2
5)
    
"CWCCWWCWCC"
55
Returns: -1
6)
    
"CWWCWCCWWC"
37
Returns: 3

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=6241

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=9989&pm=6241

Writer:

_efer_

Testers:

PabloGilberto , brett1479 , radeye , Olexiy

Problem categories:

Greedy, Simple Search, Iteration