CF1638A Reverse
CF1638A Reverse
Description
给定一个
Solution
由于是排列,所以少了许多不必要的数字重复的麻烦。
为使字典序最小,我们贪心地应该让排列前面的数字最小,也就是说我们希望的最小排列应该是
对于
找到第一个位置
这样就能保证字典序最小了,因为此时我们已经离
Code
翻转可以用 reverse,但我手写的。
void Solve()
{
n = read();
fup (i, 1, n)
a[i] = read();
int x = 0;
fup (i, 1, n)
if (i != a[i])
{
x = i;
break;
}
int l = x, r = 0;
fup (i, x, n)
if (a[i] == x)
{
r = i;
break;
}
while (l <= r)
{
swap(a[l], a[r]);
++ l, -- r;
}
fup (i, 1, n)
wsp(a[i]);
puts("");
return;
}