First Last Sorting(LST)
题目描述
a. Take any element in the list and move it to the front.
b. Take any element in the list and move it to the back.
You’ve realized that sorting speed can be improved using these transformations. For example,
consider the input list:
8, 3, 6, 7, 4, 1, 5, 2
We can do the following sequence of transformations to sort this list:
8, 3, 7, 4, 1, 5, 2, 6 (move 6 to end)
8, 3, 4, 1, 5, 2, 6, 7 (move 7 to end)
2, 8, 3, 4, 1, 5, 6, 7 (move 2 to front)
1, 2, 8, 3, 4, 5, 6, 7 (move 1 to front)
1, 2, 3, 4, 5, 6, 7, 8 (move 8 to end)
You are now curious. Given an input array of distinct values, what is the fewest number of these
first/last operations necessary to sort the array?
Given an initial permutation of the integers 1, 2, …, n, determine the fewest number of first/last operations necessary to get the list of values sorted in increasing order.
输入
输出
样例输入 Copy
8
8
3
6
7
4
1
5
2
样例输出 Copy
5
思路:求最长的连续递增的数的长度,取最大,答案为n-这个最大。(最长连续上升子序列)。
#pragma GCC optimize(3 , "Ofast" , "inline")
#include <bits/stdc++.h>
#define rep(i , a , b) for(register int i=(a);i<=(b);i++)
#define rop(i , a , b) for(register ll i=(a);i<(b);i++)
#define per(i , a , b) for(register ll i=(a);i>=(b);i--)
#define por(i , a , b) for(register ll i=(a);i>(b);i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int , int > pi;
template<class T>
inline void read (T &x) {
x = 0;
int sign = 1;
char c = getchar ();
while (c < '0' || c > '9') {
if ( c == '-' ) sign = - 1;
c = getchar ();
}
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar ();
}
x = x * sign;
}
const int maxn = 1e5+10;
const int inf = int (1e9);
const ll INF = ll(1e18);
const double PI = acos (-1);
const int mod = 1e9+7;
const double eps = 1e-8;
int n;
int p[maxn];
int vis[maxn];
int dp[maxn];
int main(){
read (n);
rep (i,1,n) {
read (p[i]);
vis[p[i]]=i;
}
int ans = 0;
rep (i,1,n) {
if(p[i]==1) dp[i]=1;
else if(vis[p[i]-1]<i) dp[i]=dp[vis[p[i]-1]]+1;
else dp[i]=1;
ans=max (ans,dp[i]);
}
printf ("%d\n",n-ans);
return 0;
}