码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 连续LIS

时间:2015-12-10 13:07:27      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

C. Sorting Railway Cars
 

An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?

Input

The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train.

The second line contains n integers pi (1 ≤ pi ≤ npi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train.

Output

Print a single integer — the minimum number of actions needed to sort the railway cars.

Sample test(s)
input
5
4 1 2 5 3
output
2
input
4
4 1 3 2
output
2
Note

In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train.

 题意:给n长度的序列,每次能把任意位置的数移到序列头,序列尾算一个操作,问你最少经过几次操作使其有序;

题解:也就是最长连续子序列

技术分享
//meek
///#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<0||ch>9)
    {
        if(ch==-)f=-1;
        ch=getchar();
    }
    while(ch>=0&&ch<=9)
    {
        x=x*10+ch-0;
        ch=getchar();
    }
    return x*f;
}
//****************************************

const int N=100000+100;
const ll inf = 1ll<<61;
const int mod= 1000000007;

int ans,a[N],H[N],n;
int dp[N];
int main() {
    ans=-1;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++) {
        H[a[i]] ++;
        if(H[a[i]-1]) dp[a[i]]=dp[a[i]-1]+1;
        else dp[a[i]]=1;
        ans=max(ans,dp[a[i]]);
    }
    cout<<n-ans<<endl;
    return 0;
代码

 

Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 连续LIS

标签:

原文地址:http://www.cnblogs.com/zxhl/p/5035349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!