Connection Status:
Competition Arena > WhiteSpaceEditing
SRM 499 · 2010-11-01 · by lyrically · Dynamic Programming, Greedy
Class Name: WhiteSpaceEditing
Return Type: int
Method Name: getMinimum
Arg Types: (vector<int>)
Problem Statement

Problem Statement

You want to type a document containing only spaces and new lines. Let SP and NL denote a space character and a new line character, respectively. You are given a int[] lines describing the desired document. The elements of lines represent the number of SP characters in each line, in order. Each line must end with a NL character. In other words, the document should look like this: lines[0] SP characters, followed by a NL, followed by lines[1] SP characters, followed by a NL, ..., lines[N-1] SP characters, followed by a NL (where N is the number of elements in lines).

The editor has a cursor, which can be positioned between two adjacent characters or at the beginning or end of the document. You can move this cursor freely.

The editor has three special keys:
  • SPACE: inserts a SP at the position of the cursor.
  • DELETE: deletes a SP character immediately to the right of the cursor. This key cannot be used if the character to the right of the cursor is a NL.
  • RETURN: inserts a NL followed by some number of SP characters. This key can only be used when the character immediately to the right of the cursor is a NL. The number of SP characters that get inserted is equal to the number of SP characters in the line where the cursor is. For example, if the document is "SP NL SP SP NL SP SP SP NL", and the cursor is immediately to the left of the second NL, it will become "SP NL SP SP NL SP SP NL SP SP SP NL" after pressing RETURN.
The document initially contains nothing but a single NL character. Return the minimum number of times you must press SPACE, DELETE or RETURN to complete the document.

Constraints

  • lines will contain between 1 and 50 elements, inclusive.
  • Each element of lines will be between 0 and 1,000,000, inclusive.
Examples
0)
{ 3, 2, 3 }
Returns: 6

You can edit the document as follows: NL SP NL SP SP NL SP SP SP NL SP SP SP NL SP SP SP NL SP SP SP NL SP SP SP NL SP SP SP NL SP SP SP NL SP SP NL SP SP SP NL

1)
{ 0 }
Returns: 0

You have to do nothing.

2)
{ 320, 187, 247, 193, 308 }
Returns: 499
3)
{ 250, 104, 204, 350 }
Returns: 499
4)
{ 1, 2, 4 }
Returns: 6

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

Coding Area

Language: C++17 · define a public class WhiteSpaceEditing with a public method int getMinimum(vector<int> lines) · 222 test cases · 2 s / 256 MB per case

Submitting as anonymous