Connection Status:
Competition Arena > ScoreRecomposition
SRM 309 · 2006-06-28 · by _efer_ · Greedy, Simple Search, Iteration
Class Name: ScoreRecomposition
Return Type: int
Method Name: minError
Arg Types: (string, int)
Problem Statement

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.

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

Submissions are judged against all 84 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class ScoreRecomposition with a public method int minError(string questions, int score) · 84 test cases · 2 s / 256 MB per case

Submitting as anonymous