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
  1. 5
    4 1 2 5 3
Output
  1. 2
Input
  1. 4
    4 1 3 2
Output
  1. 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。

代码:

  1. #include<stdio.h>
  2. int len[],n,a,i,mlen=;
  3. int main(){
  4. scanf("%d",&n);
  5. for(i=;i<=n;i++){
  6. scanf("%d",&a);
  7. len[a]=len[a-]+;
  8. if(len[a]>mlen)mlen=len[a];
  9. }
  10. printf("%d\n",n-mlen);
  11. return ;
  12. }

  

【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. 抓取天猫和淘宝的详情页图片|Golang

    taobao.go package main import ( "crypto/md5" "encoding/hex" "fmt" &quo ...

  2. POJ 1269 Intersecting Lines --计算几何

    题意: 二维平面,给两条线段,判断形成的直线是否重合,或是相交于一点,或是不相交. 解法: 简单几何. 重合: 叉积为0,且一条线段的一个端点到另一条直线的距离为0 不相交: 不满足重合的情况下叉积为 ...

  3. Codeforces 402B --耻辱的一题

    这题昨天晚上花了我1个小时50多分钟来搞,都没有搞定..后来看别人代码,直接暴力枚举第一个数的值来做..最多1000*1000的复杂度.当时怎么就没想到呢?还有为啥我的方法不对呢.. 暴力方法代码: ...

  4. 浅谈WebService返回数据效率对比

    原文链接 http://www.dotnetgeek.cn/xuexiwebservice1.html 一.什么是WebService: 简单通俗来说,就是企业之间.网站之间通过Internet来访问 ...

  5. 未能加载文件或程序集“XXXXX”或它的某一个依赖项。试图加载格式不正确的程序。

    未能加载文件或程序集“FastColoredTextBox, Version=2.10.5.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项.试图加载 ...

  6. tiff或tif文件的读取

    以下是VC下读取TIFF文件的代码 char* szFileName = "K:\\地图\\fujian-DEM\\fujian1.tif"; TIFF* tiff = TIFFO ...

  7. Linq To Entities 及其相关(进阶)

    上篇我们讲解了Linq To Entities的一些基本操作,这篇我们主要是讲解一些比较高级的东西:存储过程查询,SQL语句查询以及表达式树. 存储过程 首先来讲解存储过程查询. //Query a ...

  8. Android Home键状态保存运用场景

    当我们在一个Activity中有接收Intent过来的值,或者当前Activity有保存数据时候,如果此时不小心按到了Home键,然后没有及时回来而是运行了其它应用程序,当你想起来的时候,恐怕已经是几 ...

  9. Gradle tip #3: Tasks ordering

    I noticed that the quite often problem I face when I work with Gradle - is tasks ordering (either ex ...

  10. 学习Shell脚本编程(目录)

    所涉及的内容如下: Shell命令行的运行 编写.修改权限和执行Shell程序的步骤 在Shell程序中使用参数和变量 表达式比较.循环结构语句和条件结构语句 在Shell程序中使用函数和调用其他Sh ...