QOJ.ac

QOJ

حد الوقت: 2.0 s حد الذاكرة: 1024 MB مجموع النقاط: 100 قابلة للهجوم ✓

#17431. Up-Down Sequence

الإحصائيات

给定一个整数 $N$,确定是否存在一个由整数 $(1, 2, \dots, N)$ 组成的排列 $P = (p_1, p_2, \dots, p_N)$ 满足以下条件。

设三元组索引 $(i, j, k)$ 为满足 $1 \le i < j < k \le N$ 的任意三元组。

  • 满足 $p_i < p_j < p_k$ 的三元组索引 $(i, j, k)$ 的数量(即严格递增三元组)等于满足 $p_i > p_j > p_k$ 的三元组索引 $(i, j, k)$ 的数量(即严格递减三元组)。

如果存在这样的排列,构造并输出其中任意一个。如果不存在这样的排列,输出 $-1$。

给你 $T$ 组测试数据需要解决。

输入格式

输入按以下格式给出:

T
case_1
case_2
:
case_T

每个测试数据按以下格式给出:

N

数据范围

  • $1 \le T \le 5 \times 10^5$
  • $3 \le N \le 5 \times 10^5$
  • 所有测试数据中 $N$ 的总和不超过 $5 \times 10^5$。
  • 所有输入值均为整数。

输出格式

对于每个测试数据,如果不存在这样的排列,输出 $-1$。否则,输出 $N$ 个空格分隔的整数,表示排列 $P$ 的元素。如果存在多个这样的排列,输出其中任意一个即可。

样例

输入样例 1

2
3
4

输出样例 1

1 3 2
2 4 1 3

说明

在第一个测试数据中,$N = 3$。一个可能的有效排列是 $(1, 3, 2)$。恰好存在一个三元组索引 $(1, 2, 3)$,其对应的值为 $(1, 3, 2)$。这个三元组既不是严格递增的,也不是严格递减的。因此,递增三元组的数量为 $0$,递减三元组的数量为 $0$,满足条件。

在第二个测试数据中,$N = 4$。一个可能的有效排列是 $(2, 4, 1, 3)$。三元组索引为 $(1, 2, 3)$、$(1, 2, 4)$、$(1, 3, 4)$ 和 $(2, 3, 4)$。

对于这些三元组中的每一个,其对应的值既不是严格递增的,也不是严格递减的。因此,递增三元组的数量和递减三元组的数量均为 $0$,满足条件。

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
#1495EditorialOpen题解jiangly2026-04-09 18:15:02View
#1347EditorialOpen题解Milmon2026-03-29 20:00:34View

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.