C. Challenging Cliffs
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are a game designer and want to make an obstacle course. The player will walk from left to right. You have n heights of mountains already selected and want to arrange them so that the absolute difference of the heights of the first and last mountains is as small as possible.
In addition, you want to make the game difficult, and since walking uphill or flat is harder than walking downhill, the difficulty of the level will be the number of mountains i (1≤i<n) such that hi≤hi+1 where hi is the height of the i-th mountain. You don't want to waste any of the mountains you modelled, so you have to use all of them.
From all the arrangements that minimize |h1−hn|, find one that is the most difficult. If there are multiple orders that satisfy these requirements, you may find any.
Input
The first line will contain a single integer t (1≤t≤100) — the number of test cases. Then t test cases follow.
The first line of each test case contains a single integer n (2≤n≤2⋅105) — the number of mountains.
The second line of each test case contains n integers h1,…,hn (1≤hi≤109), where hi is the height of the i-th mountain.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
Output
For each test case, output n integers — the given heights in an order that maximizes the difficulty score among all orders that minimize |h1−hn|.
If there are multiple orders that satisfy these requirements, you may output any.
Example
inputCopy
2
4
4 2 1 2
2
3 1
outputCopy
2 4 1 2
1 3
Note
In the first test case:
The player begins at height 2, next going up to height 4 increasing the difficulty by 1. After that he will go down to height 1 and the difficulty doesn't change because he is going downhill. Finally the player will go up to height 2 and the difficulty will increase by 1. The absolute difference between the starting height and the end height is equal to 0 and it's minimal. The difficulty is maximal.
In the second test case:
The player begins at height 1, next going up to height 3 increasing the difficulty by 1. The absolute difference between the starting height and the end height is equal to 2 and it's minimal as they are the only heights. The difficulty is maximal.
E1. Erase and Extend (Easy Version)
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
This is the easy version of the problem. The only difference is the constraints on n and k. You can make hacks only if all versions of the problem are solved.
You have a string s, and you can do two types of operations on it:
Delete the last character of the string.
Duplicate the string: s:=s+s, where + denotes concatenation.
You can use each operation any number of times (possibly none).
Your task is to find the lexicographically smallest string of length exactly k that can be obtained by doing these operations on string s.
A string a is lexicographically smaller than a string b if and only if one of the following holds:
a is a prefix of b, but a≠b;
In the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
Input
The first line contains two integers n, k (1≤n,k≤5000) — the length of the original string s and the length of the desired string.
The second line contains the string s, consisting of n lowercase English letters.
Output
Print the lexicographically smallest string of length k that can be obtained by doing the operations on string s.
Examples
inputCopy
8 16
dbcadabc
outputCopy
dbcadabcdbcadabc
inputCopy
4 5
abcd
outputCopy
aaaaa
Note
In the first test, it is optimal to make one duplication: "dbcadabc" → "dbcadabcdbcadabc".
In the second test it is optimal to delete the last 3 characters, then duplicate the string 3 times, then delete the last 3 characters to make the string have length k.
"abcd" → "abc" → "ab" → "a" → "aa" → "aaaa" → "aaaaaaaa" → "aaaaaaa" → "aaaaaa" → "aaaaa".