题目 | Permutation Operations
Codeforces Global Round 23
C. Permutation Operations
https://codeforces.com/contest/1746/problem/C
2 seconds / 256 megabytes
standard input / standard output
Problem
You are given a permutation
Note that you can perform operations on the same suffix any number of times you want.
A permutation of size
Input
Each test contains multiple test cases. The first line contains the number of test cases
The first line of each test case contains a single integer
The second line contains
It's guaranteed that the sum of
Output
For each test case, print
Example
input
4
4
1 2 3 4
5
1 3 2 4 5
3
2 3 1
1
1
output
1 1 1 1
1 4 3 2 1
1 3 3
1
Note
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
In the second test case, a will be equal to
题解
首先我们要能猜到,对任意排列进行
- 对于一个数列
,令 为它的差分数列 - 原数列单调不减,即意味着它的差分数列非负,即
. - 将
加 的操作,即将差分数列中的 的值增加 . - 由于原数列为一个排列,意味着
,则 .
根据以上已知条件,我们可以构造出一种方案,保证操作后,数列为单调不减数列:
- 对于每个
( ),将 加
因为
当然,大家也可能注意到,上面括号内有条件 i % n + 1
,这种情况将会输出为
代码
- 时间复杂度:
- 空间复杂度:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int ans[MAXN];
void solve()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
{
int t;
cin >> t;
ans[t] = i % n + 1;
}
for (int i = 1; i <= n; i++)
cout << ans[i] << ' ';
cout << endl;
}
int main()
{
int t;
cin >> t;
while (t--)
solve();
return 0;
}
本文采用 CC BY-SA 4.0 许可,本文 Markdown 源码:Haotian-BiJi