第 1127 场 Codeforces Round(Div. 2,基于 2026 年墨西哥信息学奥林匹克竞赛)
C
Lavencia 橙子
Lavencia Oranges
Codeforces
未评级
原题
← BD →

Lavencia 橙子AI (CI翻译)

René is leaving Lavencia after two long, cold years, with $n$ Lavencia oranges and $n$ bags for the oranges (initially empty), both numbered from $1$ to $n$. René packs the oranges according to an array $a$: orange $i$ goes into bag $a_i$.

<题目描述> René 在 Lavencia 度过了漫长而寒冷的两年后即将离开,此时他有 $n$ 个 Lavencia 橙子和 $n$ 个用于装橙子的袋子(初始为空),橙子和袋子的编号均为 $1$ 到 $n$。René 按照数组 $a$ 装橙子:橙子 $i$ 被放入袋子 $a_i$ 中。AI (CI翻译)

Lavencia has a regulatory process for exports. Jacqueline carries it out: she chooses an integer $s$ and confiscates all bags holding exactly $s$ oranges, picking whichever $s$ takes the most oranges from René. The cost of $a$ is the number of oranges Jacqueline confiscates.

Lavencia 对出口有一套监管流程,由 Jacqueline 负责执行:她选择一个整数 $s$,没收所有装有恰好 $s$ 个橙子的袋子,并选择能从 René 那里拿走最多橙子的 $s$。数组 $a$ 的代价是 Jacqueline 没收的橙子数量。AI (CI翻译)

More formally, for a given array $a$, let $c_i$ be the number of elements that appear exactly $i$ times in $a$. The cost of $a$ is: $$ \max_{0 \le i \le n} i \cdot c_i $$

更形式化地,对于给定的数组 $a$,令 $c_i$ 表示在 $a$ 中恰好出现 $i$ 次的元素个数。数组 $a$ 的代价为:$$ \max_{0 \le i \le n} i \cdot c_i $$AI (CI翻译)

Given $n$, find an array $a$ that minimizes the cost.

给定 $n$,请构造一个能使代价最小的数组 $a$。AI (CI翻译)

Input

输入格式

AI (CI翻译)

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

每个测试包含多组测试用例。第一行包含测试用例数量 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)

The only line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$).

每个测试用例仅包含一行,其中有一个整数 $n$($1 \le n \le 2 \cdot 10^5$)。AI (CI翻译)

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

保证所有测试用例的 $n$ 之和不超过 $2 \cdot 10^5$。AI (CI翻译)

Output

输出格式

AI (CI翻译)

For each test case, output two lines.

对于每个测试用例,输出两行。AI (CI翻译)

On the first line, output a single integer — the minimum possible cost.

第一行输出一个整数——可能的最小代价。AI (CI翻译)

On the second line, output $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — any array that achieves this minimum.

第二行输出 $n$ 个整数 $a_1, a_2, \ldots, a_n$($1 \le a_i \le n$)——任意一个能达到该最小代价的数组。AI (CI翻译)

Note

说明

AI (CI翻译)

In the first test case, René places $1$, $1$, $2$, and $0$ oranges into the four bags, respectively. If Jacqueline picks size $1$, she confiscates bags $1$ and $2$, and René loses $2$ oranges. If she picks size $2$, she confiscates bag $3$, and he again loses $2$. Therefore, the cost is $2$. It can be proved that this is the minimum cost for this test case.

在第一个测试用例中,René 分别向四个袋子中放入了 $1$、$1$、$2$ 和 $0$ 个橙子。如果 Jacqueline 选择大小 $1$,她会没收袋子 $1$ 和袋子 $2$,René 损失 $2$ 个橙子。如果她选择大小 $2$,她会没收袋子 $3$,他同样会损失 $2$ 个橙子。因此,代价为 $2$。可以证明,对于这个测试用例,这是最小代价。AI (CI翻译)

In the second test case, bag $1$ holds $2$ oranges, bag $2$ holds $3$, and the other bags are empty. Picking size $2$ costs René $2$ oranges, and picking size $3$ costs him $3$, so the cost is $3$. It can be proved that, in this case, $3$ is the minimum cost.

在第二个测试用例中,袋子 $1$ 中有 $2$ 个橙子,袋子 $2$ 中有 $3$ 个橙子,其余袋子为空。选择大小 $2$ 会使 René 损失 $2$ 个橙子,选择大小 $3$ 会使他损失 $3$ 个橙子,因此代价为 $3$。可以证明,在这种情况下,$3$ 是最小代价。AI (CI翻译)

样例

Input #1
3
4
5
8
Output #1
2
1 2 3 3
3
1 1 2 2 2
3
4 2 1 5 1 3 1 3
InputCopy OutputCopy
题目信息

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。