第 1127 场 Codeforces Round(Div. 1,基于 2026 年墨西哥信息学奥林匹克竞赛)
A
拉文西亚的橙子
Lavencia Oranges
Codeforces
1300
原题
←B →

拉文西亚的橙子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é 在拉文西亚度过了漫长而寒冷的两年后,准备离开。他有 $n$ 个拉文西亚橙子和 $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.

拉文西亚对出口有一套监管流程,由 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 选择 $s=1$,她会没收袋子 $1$ 和袋子 $2$,René 会损失 $2$ 个橙子。如果她选择 $s=2$,她会没收袋子 $3$,René 同样会损失 $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$ 个,其余袋子都是空的。选择 $s=2$ 时,René 会损失 $2$ 个橙子;选择 $s=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),如有出入请以原文为准。