A15368 | Permutation Operations
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given a permutation $a$ of size $n$ and you should perform $n$ operations on it. In the $i$ -th operation, you can choose a non-empty suffix of $a$ and increase all of its elements by $i$ . How can we perform the operations to minimize the number of inversions in the final array?
Note that you can perform operations on the same suffix any number of times you want.
A permutation of size $n$ is an array of size $n$ such that each integer from $1$ to $n$ occurs exactly once in this array. A suffix is several consecutive elements of an array that include the last element of the array. An inversion in an array $a$ is a pair of indices $(i, j)$ such that $i > j$ and $a_{i} < a_{j}$ .
Note that you can perform operations on the same suffix any number of times you want.
A permutation of size $n$ is an array of size $n$ such that each integer from $1$ to $n$ occurs exactly once in this array. A suffix is several consecutive elements of an array that include the last element of the array. An inversion in an array $a$ is a pair of indices $(i, j)$ such that $i > j$ and $a_{i} < a_{j}$ .
输入格式
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.
The first line of each test case contains a single integer $n$ ( $1 \le n \le 10^5$ ) — the size of the array.
The second line contains $n$ distinct integers $a_{1}, a_{2}, \dots, a_{n}$ ( $1 \le a_i \le n$ ), the initial permutation $a$ .
It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ .
The first line of each test case contains a single integer $n$ ( $1 \le n \le 10^5$ ) — the size of the array.
The second line contains $n$ distinct integers $a_{1}, a_{2}, \dots, a_{n}$ ( $1 \le a_i \le n$ ), the initial permutation $a$ .
It's guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ .
输出格式
For each test case, print $n$ integers $x_{1}, x_{2}, \ldots, x_{n}$ ( $1 \le x_{i} \le n$ for each $1 \le i \le n$ ) indicating that the $i$ -th operation must be applied to the suffix starting at index $x_{i}$ . If there are multiple answers, print any of them.
输入输出样例
输入 #1
4 4 1 2 3 4 5 1 3 2 4 5 3 2 3 1 1 1
输出 #1
1 1 1 1 1 4 3 2 1 1 3 3 1
In the first test case one of the optimal solutions is to increase the whole array on each operation (that is, choose the suffix starting at index $1$ ). The final array $[11, 12, 13, 14]$ contains $0$ inversions.
In the second test case, $a$ will be equal to $[2, 4, 3, 5, 6]$ , $[2, 4, 3, 7, 8]$ , $[2, 4, 6, 10, 11]$ , $[2, 8, 10, 14, 15]$ and $[7, 13, 15, 19, 20]$ after the first, second, third, fourth, and fifth operations, respectively. So the final array $a$ has zero inversions.
In the second test case, $a$ will be equal to $[2, 4, 3, 5, 6]$ , $[2, 4, 3, 7, 8]$ , $[2, 4, 6, 10, 11]$ , $[2, 8, 10, 14, 15]$ and $[7, 13, 15, 19, 20]$ after the first, second, third, fourth, and fifth operations, respectively. So the final array $a$ has zero inversions.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted