Logo
(追記) (追記ここまで)

33732번 - The Best Lineup 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 2048 MB113443446.575%

문제

Farmer John has $N$ $(1 \leq N \leq 2 \cdot 10^5)$ cows in a line $a$. The $i$'th cow from the front of line $a$ is labeled an integer $a_i$ (1ドル \leq a_i \leq N$). Multiple cows may be labeled the same integer.

FJ will construct another line $b$ in the following manner:

  • Initially, $b$ is empty.
  • While $a$ is nonempty, remove the cow at the front of $a$ and potentially add that cow to the back of $b$.

FJ wants to construct line $b$ such that the sequence of labels in $b$ from front to back is lexicographically greatest (see the footnote).

Before FJ constructs line $b,ドル he can perform the following operation at most once:

  • Choose a cow in line $a$ and move it anywhere before its current position.

Given that FJ optimally performs the aforementioned operation at most once, output the lexicographically greatest label sequence of $b$ he can achieve.

Each input will consist of $T$ (1ドル \leq T \leq 100$) independent test cases.

입력

The first line contains $T$.

The first line of each test case contains $N$.

The second line of each test case contains $N$ space-separated integers $a_1, a_2, \ldots, a_N$.

It is guaranteed that the sum of $N$ over all test cases does not exceed 10ドル^6$.

출력

For each test case, output the lexicographically greatest $b$ on a new line.

제한

예제 입력 1

3
5
4 3 2 1 3
6
5 1 2 6 3 4
6
4 1 3 2 1 1

예제 출력 1

4 3 3 2 1
6 5 4
4 3 2 1 1

In the first test case, FJ can move the fifth cow to directly after the second cow. Now, $a = [4, 3, 3, 2, 1]$. It can be shown $[4, 3, 3, 2, 1]$ is also the lexicographically greatest $b$.

In the second test case, FJ can move the fourth cow to the front of the line.

In the third test case, FJ does not need to perform any operations. He can construct $b$ by adding each cow beside the second cow to the back of $b$. It can be shown this results in the lexicographically greatest $b$.

노트

Recall that a sequence $s$ is lexicographically greater than a sequence $t$ if and only if one of the following holds:

  • At the first position $i$ where $s_i \neq t_i,ドル $s_i > t_i$.
  • If no such $i$ exists, $s$ is longer than $t$.

출처

Olympiad > USA Computing Olympiad > 2024-2025 Season > USACO 2025 February Contest > Silver 1번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /