RowsOrdering
SRM 516 · 2011-05-25 · by dolphinigle
Problem Statement
The rows of the table can be sorted using the following scheme:
- First, for each column, assign a permutation of all the integers between 1 and 50, inclusive. Note that this permutation may differ for each column.
- Next, pick a permutation of all M columns.
- Finally, sort the rows in ascending order. Compare each pair of rows as follows:
- Consider the first column in the permutation chosen in step 2. Compare the values in both rows in this column.
- If the values in this column are the same in both rows, check the values in the next column in the permutation instead. Repeat this until the values for the chosen column are different.
- The row which contains the value that comes earlier in the permutation chosen in step 1 for this column is considered to be smaller than the other row.
- 'a'-'z' corresponds to values 1-26
- 'A'-'X' corresponds to values 27-50
Notes
- The number that you should minimize is the number before the modulo and not after the modulo. That is, 1,000,000,010 is larger than 20 even though 1,000,000,010 modulo 1,000,000,007 = 3 < 20.
- Duplicate elements are allowed in rows. In case rows contains C > 1 occurrences of a certain row, the value of P for this row must be added towards S for C times (see example 4).
Constraints
- rows will contain between 1 and 50 elements, inclusive.
- Each element of rows will contain between 1 and 50 characters, inclusive.
- All the elements of rows will contain the same number of characters.
- Each character in rows will be either 'a'-'z' or 'A'-'X'.
{"bb", "cb", "ca"}
Returns: 54
One possible ordering is as follows: For the first column, choose the permutation {c, b, ...}. For the second column, choose the permutation {b, a, ...}. Then, choose the permutation {1, 0} for the columns. The rows will then be indexed as follows: "cb" = 1 "bb" = 2 ... "ca" = 51 "ba" = 52 ... The sum is 1+2+51 = 54.
{"abcd", "ABCD"}
Returns: 127553
Lowercase and uppercase letters are considered distinct.
{"dolphinigle", "ivanmetelsk", "lympandaaaa"}
Returns: 356186235
{"Example", "Problem"}
Returns: 943877448
{"a", "b", "c", "d", "e", "f", "g"}
Returns: 28
{"a", "a"}
Returns: 2
Duplicate elements are allowed. The value of P for each such element should be added towards S separately.
Submissions are judged against all 150 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class RowsOrdering with a public method int order(vector<string> rows) · 150 test cases · 2 s / 256 MB per case