Finding a string with enough inversions
Time limit2sMemory limit512 MB
Find the lexicographically smallest permutation of the first N lowercase letters with at least V inversions that is not smaller than the given string S.
- Level
Medium7 of 10
- Topics
- Backtracking, Combinatorics, Greedy
- Solved
- No attempts yet
Problem
For a string of length , the number of inversions in is the number of pairs with and . The first character of a string is character 0. For example, "abcab" has three inversions: , , .
You are given integers and and a string . Consider the strings of length that use each of the first lowercase letters exactly once, that is, the permutations of those letters. Call such a string when it meets both conditions below.
- The number of inversions in is at least .
- does not come before in lexicographic order.
Find the that comes first in lexicographic order.
Input
The first line contains ().
The second line contains ().
The third line contains the string . is made of some of the first lowercase letters, and no letter appears twice. The length of is at least 1 and at most .
Output
Print an that meets the conditions on the first line. If no such exists, print -1.