A. Sorting Railway Cars
2 seconds
256 megabytes
standard input
standard output
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?
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.
Print a single integer — the minimum number of actions needed to sort the railway cars.
5
4 1 2 5 3
2
4
4 1 3 2
2
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.
大意:有n个数字组成的序列,每次可以把序列中的某个数放到开头或者尾部,问最少需要多少次操作才能使序列从小到大排列
分析:我们只需要找到最大的一段不需要移动的序列//即连续的差1的序列 就可以用n-ans得到答案
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100005
using namespace std; int max(int a,int b)
{
if(a>b)return a;
else return b;
} int main()
{
int n;
cin>>n;
int pos[maxn],a[maxn];
for(int i=;i<=n;++i)
{
scanf("%d",&a[i]);
pos[a[i]]=i;
}
int cnt=,ans=;
for(int i=;i<=n;++i)
{
if(pos[i]>pos[i-])cnt++;
else cnt=;
ans=max(ans,cnt);
}
cout<<n-ans;
puts("");
return ;
}
A. Sorting Railway Cars的更多相关文章
- CF#335 Sorting Railway Cars
Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 连续LIS
C. Sorting Railway Cars An infinitely long railway has a train consisting of n cars, numbered from ...
- 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 ...
- Codeforces 606-C:Sorting Railway Cars(LIS)
C. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces Round #335 (Div. 2) C. Sorting Railway Cars
C. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 335C Sorting Railway Cars
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【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 lon ...
- [Codeforces 606C]Sorting Railway Cars
Description An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the n ...
- CodeForces 606C Sorting Railway Cars(最长连续上升子序列)
Description An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the n ...
随机推荐
- sql server 查询多个不关联表且对结果编号
1.除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图.内联函数.派生表.子查询和公用表表达式中无效. 解决方法:top 100 percent * 2.如何对查询结果编 ...
- sublime3 注册码
—– BEGIN LICENSE —– Michael Barnes Single User License EA7E-821385 8A353C41 872A0D5C DF9B2950 AFF6F6 ...
- [react native] Error loading page
如上图显示的错误,解决方法如下: 在react native ios项目的info.plist文件中,新增一个属性. 在Info.plist中添加NSAppTransportSecurity类型Dic ...
- .NET Framework 3.5 安装错误:0x800F0906、0x800F081F、0x800F0907
使用Add-WindowsFeature 照成的问题 I get the failure below.. If I pick the Server 2012 R2 image from 8/15/2 ...
- erp与电子商务集成的结构图
集约化采购管理系统和电子商务平台统一规划.统一设计,通过系统之间的安全接口全面集成,进而实现资源共享和数据共享,企业内外部系统运作的一体化,建立企业同上.下游合作伙伴的电子数据交互,从而提高电子商务的 ...
- Java之this详解
1. this是指当前对象自己. 用类名定义一个变量的时候,定义的应该只是一个引用,外面可以通过这个引用来访问这个类里面的属性和方法,那们类里面是够也应该有一个引用来访问自己的属性和方法纳?呵呵,JA ...
- Protocol Buffer使用
Protocol Buffer使用简介 字数2630 阅读5067 评论1 喜欢12 我们项目中使用protocol buffer来进行服务器和客户端的消息交互,服务器使用C++,所以本文主要描述pr ...
- JAVA类与对象(三)----类定义关键字详解
static 表示静态,它可以修饰属性,方法和代码块. 1.static修饰属性(类变量),那么这个属性就可以用类名.属性名来访问,也就是使这个属性成为本类的类变量,为本类对象所共有.这个属性就是全类 ...
- DB天气app冲刺二阶段第七天
又冲刺了一个礼拜了 今天收获应该算是不小了 虽然进度上来说还是一点也没前进 但是找到了好几个突破口 明天继续 今天先不多说了困了..
- beego 0.9.0 中智能路由AutoRouter的使用方法及源码解读
了解beego的开发者肯定知道,beego的路由设计来源于sinatra,原来是不支持自动路由的,每一个路由都要自己配置的,如: type MainController struct { beego. ...