codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1537C.md (2574B)


      1 Source: https://codeforces.com/problemset/problem/1537/C
      2 
      3 C. Challenging Cliffs
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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.
     22 
     23 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 \leq i \lt n$) such that $h_i \leq h_{i+1}$ where $h_i$ 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.
     24 
     25 From all the arrangements that minimize $|h_1-h_n|$, find one that is the most difficult. If there are multiple orders that satisfy these requirements, you may find any.
     26 
     27 Input
     28 
     29 The first line will contain a single integer $t$ ($1 \leq t \leq 100$) — the number of test cases. Then $t$ test cases follow.
     30 
     31 The first line of each test case contains a single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of mountains.
     32 
     33 The second line of each test case contains $n$ integers $h_1,\ldots,h_n$ ($1 \leq h_i \leq 10^9$), where $h_i$ is the height of the $i$-th mountain.
     34 
     35 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     36 
     37 Output
     38 
     39 For each test case, output $n$ integers — the given heights in an order that maximizes the difficulty score among all orders that minimize $|h_1-h_n|$.
     40 
     41 If there are multiple orders that satisfy these requirements, you may output any.
     42 
     43 Example
     44 
     45 Input
     46 
     47 ```
     48 2
     49 4
     50 4 2 1 2
     51 2
     52 3 1
     53 ```
     54 
     55 Output
     56 
     57 ```
     58 2 4 1 2 
     59 1 3
     60 ```
     61 
     62 Note
     63 
     64 In the first test case:
     65 
     66 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.
     67 
     68 In the second test case:
     69 
     70 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.