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;
    代码
  • 相关阅读:
    PHP cURL 函数
    PHP 5 Calendar 函数
    PHP 5 Array 函数
    PHP 实例 AJAX 投票
    PHP 实例 AJAX RSS 阅读器
    PHP 实例
    PHP 实例 AJAX 与 XML
    PHP 实例 AJAX 与 MySQL
    22_传智播客iOS视频教程_类的定义
    21_传智播客iOS视频教程_类的设计和名词提炼法
  • 原文地址:https://www.cnblogs.com/zxhl/p/5035349.html
Copyright © 2011-2022 走看看