Inaka composes music. Today's arrangement includes a chord of n notes that are pairwise distinct, represented by a permutation p1…n of integers from 1 to n (inclusive) denoting the notes from the lowest to the highest.
Her friend, Miyako, sneaks in and plays a trick by altering the chord with the following two operations:
- Drop-2: Take out the second highest note and move it to the lowest position, i.e. change the permutation to pn−1,p1,p2,…,pn−3,pn−2,pn.
- Invert: Take out the lowest note and move it to the highest position, i.e. change the permutation to p2,p3,…,pn−1,pn,p1.
Any number of consecutive Drop-2 operations is considered a multi-drop. Miyako would like to change the permutation to an ordered permutation, 1,2,…,n, in the fewest number of multi-drops possible. Please help her find the number of multi-drops needed.
思路:
本题通过连续的Drop-2与Invert交换任意两个数字的位子,所以此题可以简化为区间最大上升子序列。
AC代码:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1e4 + 10;
int n,a[N],ans=1e9;
int f[N];
int work() {
int res = 0;
for(int i = 1; i <= n;i++){
f[i] = 1;
for(int j = 1;j<i;j++)
if(a[i] > a[j]) f[i] = max(f[i],f[j] + 1);
res = max(res, f[i]);
}
return res;
}
void move() {
int ans = a[1];
for (int i = 1; i < n; i ++ )
a[i] = a[i + 1];
a[n] = ans;
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++ ) cin >> a[i];
for (int i = 1; i <= n; i ++ )
ans = min(ans, n - work()), move();
cout << ans;
}