zoukankan      html  css  js  c++  java
  • Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 连续LIS

    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;
    代码
  • 相关阅读:
    【转】linux清屏的几种方法
    【转】Ubuntu 64位系统安装交叉编译环境一直提醒 没有那个文件或目录
    【转】无法获得锁 /var/lib/dpkg/lock
    层级原理图设计方法
    【转】gcc 编译使用动态链接库和静态链接库
    【转】设置 vim 显示行号永久有效
    【转】VMware 全屏显示
    emwin之自绘制 BUTTON 图形的一些问题
    使用 sizeof 获取字符串数组的大小
    emwin之2D图形流位图显示的方法
  • 原文地址:https://www.cnblogs.com/zxhl/p/5035349.html
Copyright © 2011-2022 走看看