http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/E

Description

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 Input

Input
5
4 1 2 5 3
Output
2
Input
4
4 1 3 2
Output
2

Hint

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.

题解:找到这些数中最长的连续的序列(形如123、234、45678),长度为mlen,就是差值为1的上升序列,然后答案就是n-mlen。

因为总共n个数,每个1..n都会出现一次,len[i]表示以i结尾的连续序列的最大长度,则len[a]=len[a-1]+1。

代码:

#include<stdio.h>
int len[],n,a,i,mlen=;
int main(){
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%d",&a);
len[a]=len[a-]+;
if(len[a]>mlen)mlen=len[a];
}
printf("%d\n",n-mlen);
return ;
}

  

【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars的更多相关文章

  1. 【CodeForces 312B】BUPT 2015 newbie practice #3A Archer

    题 SmallR is an archer. SmallR is taking a match of archer with Zanoes. They try to shoot in the targ ...

  2. 【UVALive 3905】BUPT 2015 newbie practice #2 div2-D-3905 - Meteor

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/D The famous Korean internet co ...

  3. 【HDU 4925】BUPT 2015 newbie practice #2 div2-C-HDU 4925 Apple Tree

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/C Description I’ve bought an or ...

  4. 【UVA 401】BUPT 2015 newbie practice #2 div2-B-Palindromes

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/B A regular palindrome is a str ...

  5. 【UVA 11078】BUPT 2015 newbie practice #2 div2-A -Open Credit System

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/A In an open credit system, the ...

  6. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  7. Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 动态规划

    C. Sorting Railway Cars Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/conte ...

  8. 【最大流】ECNA 2015 F Transportation Delegation (Codeforces GYM 100825)

    题目链接: http://codeforces.com/gym/100825 题目大意: N(N<=600)个点,每个点有个名字Si,R(R<=200)个生产商在R个点上,F(F<= ...

  9. 【宽搜】ECNA 2015 D Rings (Codeforces GYM 100825)

    题目链接: http://codeforces.com/gym/100825 题目大意: 给你一张N*N(N<=100)的图表示一个树桩,'T'为年轮,'.'为空,求每个'T'属于哪一圈年轮,空 ...

随机推荐

  1. 2014 Super Training #7 E Calculate the Function --矩阵+线段树

    原题:ZOJ 3772 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3772 这题算是长见识了,还从没坐过矩阵+线段树的题 ...

  2. 分层开发(MySchool总结)

    由于分层之间存在各层之间的关系窗体之间的方法跳转,故有需要者可以进行下载本地文件 MySchool.rar 3304KB 5/22/2016 9:43:28 AM ,代码中有注释, 上述代码,属个人所 ...

  3. PCTF-2016-WEB

    Pctf ** web100 PORT51**  开始看到这个真的无法下手,想过用python–socket编程或者scapy发包.自己觉得是可以的,但是没有去试,后面看一大神writeup,知道: ...

  4. ng-bind的使用

    由于JS是单线程的,当HTML页面执行alert的时候,会中断下面代码的运行,所以为了良好的用户体验,当需要在页面使用{{name}}的时候,通常不这样直接输出,而是用ng-bind绑定model数据 ...

  5. localStroage的用法

    Cookie 每个域名存储量比较小(各浏览器不同,大致4K) 所有域名的存储量有限制(各浏览器不同,大致4K) 有个数限制(各浏览器不同) 会随请求发送到服务器 LocalStorage 永久存储 单 ...

  6. vrrp两用

    早上想了想vrrp的使用,1,网关冗余 2,服务器热备 思想稍微有点不一样.主要在于监控口 服务器的话有心跳线,用户同步一些配置和迁移一些服务.达到热备的目的.:牵涉到四个优先级:建议这样排序: 主机 ...

  7. C++创建对象的两种方式

    C++创建对象有两种方式,在栈上创建对象(Objects on the Stack)和在堆上创建对象(Objects on the Heap). 假设我们有以下的类: #include <str ...

  8. 一个Eclipse代码显示主题

  9. dxut.h(29): fatal error C1083: Cannot open include file: 'dxsdkver.h': No such file or directory

    从网上download一个三维演示模型的软件编译发现报找不到dxsdkver.h文件,网上查阅这是MS的DirectX sdk中的库文件,于是先download DirectX SDK 安装之后,配置 ...

  10. File类和RandomAccessFile类

    目录 File类     File类常用操作     (1)创建文件     (2)删除文件     (3)创建文件夹     (4)列出指定目录全部文件     (5)删除目录 RandomAcce ...