First Last Sorting(LST)

First Last Sorting(LST)

题目描述

Arup has just created a data structure that makes the two following list transformations in constant O(1) time:

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.

输入

The first line of input will contain a single positive integer, n (n ≤ 10 5 ), representing the number of values to be sorted.  The next n lines contain one integer each.  All of these integers will be distinct values in between 1 and n (inclusive), representing the original order of the data to sort for the input case.

输出

On a line by itself, output the fewest number of first/last operations necessary to sort the input list.

样例输入 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;
}
发布了259 篇原创文章 · 获赞 2 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/dy416524/article/details/105734003