【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'属于哪一圈年轮,空 ...
随机推荐
- 如何解决python中urlopen超时问题
看代码: 利用urlopen中的超时参数设立一个循环 while True: try: page = urllib.request.urlopen(url, timeout=3) break exce ...
- 第1章 UML基础:类的关系
1. 类的关系 1.1 继承和实现:继承表示有父子关系 1.2 依赖:(use–a),表示一个类要使用(use)另一个类. (1)类图 (2)三种依赖方式:函数参数或返回值.局部变量和静态成员变量或函 ...
- java14-9 Doteformat的练习
需求: 键盘录入出生年月日,计算出距离现在已经生活了几天 分析: A:创建键盘录入固定模式的字符串 B:计算步骤: a:把输入进来的字符串格式化成日期 b:获取现在的日期,减去格式化后的日期 c:把得 ...
- jquery的children方法和css3选择器配合使用
$(".pid").children("ul:nth-child(2)");//获取拥有pid类元素下的第二个ul元素 $(".pid"). ...
- 最短路径—Dijkstra算法
Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...
- 使用Apache ab进行http性能测试
Mac自带了Apache环境 打开“终端(terminal)”,输入 sudo apachectl -v,(可能需要输入机器秘密).如下显示Apache的版本 接着输入 sudo apachectl ...
- 静态时序分析(static timing analysis) --- 时序路径
时序分析工具会找到且分析设计中的所有路径.每一个路径有一个起点(startpoint)和一个终点(endpoint).起点是设计中数据被时钟沿载入的那个时间点,而终点则是数据通过了组合逻辑被另一个时间 ...
- C语言 百炼成钢4
//题目10:打印楼梯,同时在楼梯上方打印两个笑脸. #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdli ...
- 各种同步方法性能比较(synchronized,ReentrantLock,Atomic)
5.0的多线程任务包对于同步的性能方面有了很大的改进,在原有synchronized关键字的基础上,又增加了ReentrantLock,以及各种Atomic类.了解其性能的优劣程度,有助与我们在特定的 ...
- U3D assetbundle加载与卸载的深入理解
using UnityEngine; using System.Collections; using System; public class testLoadFromAB : MonoBehavio ...