zoukankan      html  css  js  c++  java
  • dp

    A. Sorting Railway Cars
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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 ≤ n, pi ≠ 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.

    Examples
    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.

    题目大意 :

      每次只能将一个汽车移动到序列最前或最后 , 问最小移动次数 。

    思路 : 只要我找到 这串数字的 最长连续数 的个数 , 如 1 3 2 4 5 , 可以看出这段数的最长连续数的个数是 3 个 , 因此递推的状态转移方程是 dp[pre[i]] = dp[pre[ i ] - 1] + 1;

    代码示例 :

     

    /*
     * Author:  ry 
     * Created Time:  2017/9/28 14:25:17
     * File Name: 1.cpp
     */
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <set>
    #include <time.h>
    using namespace std;
    const int eps = 1e5+5;
    const double pi = acos(-1.0);
    #define Max(a,b) a>b?a:b
    #define Min(a,b) a>b?b:a
    #define ll long long
    
    int pre[eps];
    int dp[eps];
    
    int main() {
        int n;
        
        while (~scanf("%d", &n)){
            memset(dp, 0, sizeof(dp));
            for(int i = 1; i <= n; i++){
                scanf("%d", pre+i);
            }
            for(int i = 1; i <= n; i++)
                dp[pre[i]] = dp[pre[i]-1] + 1;
            printf("%d
    ", n - *max_element(dp+1, dp+1+n));
        }
    
        return 0;
    }
    

    /*
     * Author:  ry 
     * Created Time:  2017/9/28 14:25:17
     * File Name: 1.cpp
     */
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <set>
    #include <time.h>
    using namespace std;
    const int eps = 1e5+5;
    const double pi = acos(-1.0);
    #define Max(a,b) a>b?a:b
    #define Min(a,b) a>b?b:a
    #define ll long long
    
    int pre[eps];
    int dp[eps];
    
    int main() {
        int n;
        
        while (~scanf("%d", &n)){
            memset(dp, 0, sizeof(dp));
            for(int i = 1; i <= n; i++){
                scanf("%d", pre+i);
            }
            for(int i = 1; i <= n; i++)
                dp[pre[i]] = dp[pre[i]-1] + 1;
            printf("%d
    ", n - *max_element(dp+1, dp+1+n));
        }
    
        retu
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    Java发送HTTP的Get 和 Post请求
    vue 中使用 Ant Design 依次提供了三级选项卡
    Postman中不为人知的秘密 之 设置全局变量,token
    vue组件之间传值(03)__兄弟组件传值,事件总线[ EventBus ]
    元素内部滚动到底部和顶部的监听
    如何将三个数的颜色色值兼容成六个数的方法
    前端内容的自动化构建
    模拟vue实现简单的webpack打包
    VXcode学习
    npm install 成功安装依赖后,运行跑不起来怎么办?
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/7606590.html
Copyright © 2011-2022 走看看