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;
}
原文地址:https://www.cnblogs.com/yinghualuowu/p/5037291.html