zoukankan      html  css  js  c++  java
  • Codeforces Round #335 (Div. 2) C

                                                                   C. 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 ≤ 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.

    找他们最大的连续长度 比如 4 1 2 5 3中的 1 2 3 然后总长度减去就行 我们用数组去存这个数字的上一个数字出现的位置 取最大

    #include<stdio.h>
    //#include<bits/stdc++.h>
    #include<string.h>
    #include<iostream>
    #include<math.h>
    #include<sstream>
    #include<set>
    #include<queue>
    #include<map>
    #include<vector>
    #include<algorithm>
    #include<limits.h>
    #define inf 0x3fffffff
    #define INF 0x3f3f3f3f
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define ULL unsigned long long
    using namespace std;
    int i,j;
    int n,m;
    int sum,ans,flag;
    int num;
    int a;
    map<int,int> q;
    int main()
    {
        cin>>n;
        ans=1;
        sum=0;
        q[0]=0;
        for(i=1;i<=n;i++)
        {
            cin>>a;
            q[a]=q[a-1]+1;
            sum=max(sum,q[a]);
        }
        cout<<n-sum<<endl;
        return 0;
    }
  • 相关阅读:
    poj 3304 Segments 直线 线段求交
    poj 1077 Eight 八数码 A*算法
    UESTC 1447 Area 凸包+旋转卡壳 求最大四边形面积
    ACM计算几何题目推荐(第二期)
    poj 2398 Toy Storage 叉乘
    ACM计算几何题目推荐 (第一期)
    (转载)Telnet协议详解及使用C# 用Socket 编程来实现Telnet协议
    jquery 表情编辑器
    (读书笔记)Asp.net Mvc 与WebForm 混合开发
    (转载)精简说明C#最基本的Socket编程示例
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/5037291.html
Copyright © 2011-2022 走看看