codeforces 335 C Sorting Railway Cars

来源:互联网 发布:java物流管理系统 编辑:程序博客网 时间:2024/05/18 02:18

题目链接:这里。

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

54 1 2 5 3

Output

2

Input

44 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~n之间而且没有重复的。

【分析】

直觉就想到了最长上升子序列啊,然后看了一下测试用例,刚好符合,就以为想法没有问题,然后直接写了一发,结果wa21.难受,然后仔细推敲一下,发现:

首先就是最长上升子序列的那一部分,显然是不用交换的,然而,我们神奇的发现了,如果所求的最长上升子序列中相邻的两项如果不是严格增加为一的话,最后还是要换走的,所以这样求出来的并不是最后的结果。所以在每次统计答案的时候我们只统计严格增加为一的最长上升子序列。然后用总长度减去所求的的最长上升子序列的长度就是答案。

【代码】

#include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<set>#include<map>#include<queue>#include<sstream>#define rep0(i,l,r) for(int i = (l);i < (r);i++)using namespace std;#define rep1(i,l,r) for(int i = (l);i <= (r);i++)#define rep_0(i,r,l) for(int i = (r);i > (l);i--)#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)#define MS0(a) memset(a,0,sizeof(a))#define MS_1(a) memset(a,-1,sizeof(a))#define MSinf(a) memset(a,0x3f,sizeof(a))#define MSfalse(a) memset(a,false,sizeof(a))#define pin1(a) printf("%d",(a))#define pin2(a,b) printf("%d %d",(a),(b))#define pin3(a,b,c) printf("%d %d %d",(a),(b),(c))#define pll1(a) printf("%lld",(a))#define pll2(a,b) printf("%lld %lld",(a),(b))#define pll3(a,b,c) printf("%lld %lld %lld",(a),(b),(c))#define pdo1(a) printf("%f",(a))#define pdo2(a,b) printf("%f %f",(a),(b))#define pdo3(a,b,c) printf("%f %f %f",(a),(b),(c))#define huiche puts("")#define inf 0x3f3f3f3f#define lson (ind<<1),l,mid#define rson ((ind<<1)|1),mid+1,r#define uint unsigned inttypedef pair<int,int> PII;#define A first#define B second#define pb push_back#define mp make_pair#define ll long long#define eps 1e-8#define pi acos(-1.0)inline void read1(int &num) {    char in;    bool IsN=false;    in=getchar();    while(in!='-'&&(in<'0'||in>'9')) in=getchar();    if(in=='-') {        IsN=true;        num=0;    } else num=in-'0';    while(in=getchar(),in>='0'&&in<='9') {        num*=10,num+=in-'0';    }    if(IsN) num=-num;}inline void read2(int &a,int &b) {    read1(a);    read1(b);}inline void read3(int &a,int &b,int &c) {    read1(a);    read1(b);    read1(c);}inline void read1(ll &num) {    char in;    bool IsN=false;    in=getchar();    while(in!='-'&&(in<'0'||in>'9')) in=getchar();    if(in=='-') {        IsN=true;        num=0;    } else num=in-'0';    while(in=getchar(),in>='0'&&in<='9') {        num*=10,num+=in-'0';    }    if(IsN) num=-num;}inline void read2(ll &a,ll &b) {    read1(a);    read1(b);}inline void read3(ll &a,ll &b,ll &c) {    read1(a);    read1(b);    read1(c);}inline void read1(double &num) {    char in;    double Dec=0.1;    bool IsN=false,IsD=false;    in=getchar();    while(in!='-'&&in!='.'&&(in<'0'||in>'9'))        in=getchar();    if(in=='-') {        IsN=true;        num=0;    } else if(in=='.') {        IsD=true;        num=0;    } else num=in-'0';    if(!IsD) {        while(in=getchar(),in>='0'&&in<='9') {            num*=10;            num+=in-'0';        }    }    if(in!='.') {        if(IsN) num=-num;        return ;    } else {        while(in=getchar(),in>='0'&&in<='9') {            num+=Dec*(in-'0');            Dec*=0.1;        }    }    if(IsN) num=-num;}inline void read2(double &a,double &b) {    read1(a);    read1(b);}inline void read3(double &a,double &b,double &c) {    read1(a);    read1(b);    read1(c);}///---------------------------------------------------------------------------------------------------------------const int maxm = 1e5+100;int num[maxm];int dp[maxm];int len;int main() {//    freopen("in.txt","w+",stdout);    while(scanf("%d",&len)!=EOF)    {        rep0(i,0,len)            read1(num[i]);        MS0(dp);        int ans = 0;        rep0(i,0,len)        {            dp[num[i]] = dp[num[i]-1]+1;            ans = max(dp[num[i]],ans);        }        printf("%d\n",len-ans);    }      return 0;}