题目收集
题目列表题单
返回列表

Bad-Looking Subsegments

CodeforcesP0044· 2000ms· 1024 MB· ZJU Summer 2026 Contest 4原题链接
贪心模拟数据结构优化

创建于 2026/07/17 · 更新于 2026/07/17

题目描述

Ostad gave Nostad an array aaa of length nnn as a birthday gift. However, Nostad thinks that the array is not beautiful because it contains too many bad-looking subsegments.

Nostad considers a subsegment

[al,al+1,⋯ ,ar][a_l, a_{l+1}, \cdots, a_r] [al​,al+1​,⋯,ar​]

to be bad-looking if it satisfies at least one of the following conditions:

  1. al≤al+1≤⋯≤ara_l \le a_{l+1} \le \cdots \le a_ral​≤al+1​≤⋯≤ar​, i.e., the subsegment is a non-decreasing sequence.
  2. al≥al+1≥⋯≥ara_l \ge a_{l+1} \ge \cdots \ge a_ral​≥al+1​≥⋯≥ar​, i.e., the subsegment is a non-increasing sequence.

Nostad plans to remove the elements of the array one by one until the array contains exactly one element. In each operation, he chooses one element of the current array, removes it, and then concatenates the remaining parts.

Before performing any removal and after each removal operation, Nostad computes the length of the longest bad-looking subsegment of the current array and writes it down. As a result, after the array is reduced to a single element, he obtains a sequence

l=l1,l2,⋯ ,ln,l = l_1, l_2, \cdots, l_n, l=l1​,l2​,⋯,ln​,

where l1l_1l1​ denotes the length of the longest bad-looking subsegment of the initial array, and for each i≥2i \ge 2i≥2, lil_ili​ denotes the length of the longest bad-looking subsegment after the (i−1)(i-1)(i−1)-th removal.

Since Nostad wants to make the array beautiful as fast as possible, he wants to choose the order of deletions such that the sequence lll is lexicographically minimum.

Your task is to compute the lexicographically minimum possible sequence lll.

输入格式

The first line contains an integer ttt (1≤t≤104)(1 \le t \le 10^4)(1≤t≤104), the number of test cases.

For each test case:

  • The first line contains an integer nnn (2≤n≤106)(2 \le n \le 10^6)(2≤n≤106), the length of the array.
  • The second line contains nnn integers a1,a2,⋯ ,ana_1, a_2, \cdots, a_na1​,a2​,⋯,an​ (1≤ai≤109)(1 \le a_i \le 10^9)(1≤ai​≤109), representing the array aaa.

It is guaranteed that the sum of nnn over all test cases does not exceed 10610^6106.

输出格式

For each test case, print a single line containing nnn integers

l1,l2,⋯ ,ln,l_1, l_2, \cdots, l_n, l1​,l2​,⋯,ln​,

where lll is the lexicographically minimum sequence of longest bad-looking subsegment lengths that Nostad can obtain for that test case.

样例输入 #1

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

样例输出 #1

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