【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars
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 ≤ 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.
Sample Input
5
4 1 2 5 3
2
4
4 1 3 2
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的更多相关文章
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 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 ...
- 【最大流】ECNA 2015 F Transportation Delegation (Codeforces GYM 100825)
题目链接: http://codeforces.com/gym/100825 题目大意: N(N<=600)个点,每个点有个名字Si,R(R<=200)个生产商在R个点上,F(F<= ...
- 【宽搜】ECNA 2015 D Rings (Codeforces GYM 100825)
题目链接: http://codeforces.com/gym/100825 题目大意: 给你一张N*N(N<=100)的图表示一个树桩,'T'为年轮,'.'为空,求每个'T'属于哪一圈年轮,空 ...
随机推荐
- MATLAB基本命令
eye(n)创建n阶单位矩阵 zeros(n)创建n阶0方阵 rand(m,n)创建m*n阶元素为从0到1的均匀分布的随机数矩阵 round(A)对矩阵A中所有元素进行四舍五入运算 A^-1用幂运算求 ...
- AC日记——潜伏者 洛谷 P1071 (模拟)
题目描述 R 国和 S 国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历尽艰险后,潜伏于 S 国的 R 国间谍小 C 终于摸清了 S 国军用密码的编码规则: 1. S 国军方内部欲发送的原 ...
- Unity-WIKI 之 DebugConsole
功能预览 使用说明 1.把 DebugConsole.cs 放在 Standard Assets 目录下(重要) 2.如果你想修改 DebugConsole的一些参数,把DebugConsole.cs ...
- Android Studio系列教程六--Gradle多渠道打包
Android Studio系列教程六--Gradle多渠道打包 2015 年 01 月 15 日 DevTools 本文为个人原创,欢迎转载,但请务必在明显位置注明出处!http://stormzh ...
- JavaScript---基本语法
字符串方法:str.lengthstr.charAt(i):取字符串中的某一个;str.indexOf('e');找第一个出现的位置;找不到返回-1;str.lastIndexOf('e'):找最后一 ...
- velocity .vm
关于.vm 后缀的文件,是velocity的文件.velocity是基于java的一种页面模板引擎,支持#if #else #foreach等写法的前台文件.$link.contextPath是该引擎 ...
- 19SpringMvc_在业务控制方法中收集List集合中包含JavaBean参数
本文要实现的功能是给一张表单:
- 第三章 Models模块属性详解
摘自:http://www.cnblogs.com/xdotnet/archive/2012/03/07/aspnet_mvc40_validate.html 了解了这些就可以对MVC进一步认识,相信 ...
- C/C++关于string.h头文件和string类
学习C语言时,用字符串的函数例如stpcpy().strcat().strcmp()等,要包含头文件string.h 学习C++后,C++有字符串的标准类string,string类也有很多方法,用s ...
- tiff或tif文件的读取
以下是VC下读取TIFF文件的代码 char* szFileName = "K:\\地图\\fujian-DEM\\fujian1.tif"; TIFF* tiff = TIFFO ...