CF605A Sorting Railway Cars(递推)
题目描述
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<=100000 ) — 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.
题意翻译
题意
给出一个 1 到 n 的排列,每次操作可以将某个位置的数字移动到最前面或最后面,求将排列从小到大排序的最小操作次数
如:4 1 2 5 3 操作1:将5和3换一下变成4 1 2 3 5
操作2:将1 2 3和 4换一下变成 1 2 3 4 5 在此后即完成要求。所以最小操作次数为2。
输入:
第一行 为长度 nnn ; 第二行为原来的顺序两个数之间有空格。
输出:
最小操作次数
范围
1<=n<=100000
1=<p[i]<=n
p[i]!=p[j] 当 i!=j
思路:
代码极短,思路可不简单
每次往队首或往队末放一辆车,且要求最后车都按顺序排列
那么这个问题首先就要考虑无效操作
如果你的当前这辆车编号比当前位置小,
那你显然应该往前放
如果你往后放那你将来要不把前面的往后放
要么再把这个放回去
明显不会更优
所以这个问题最后转化成了在这个序列里最多有哪些车可以不用改变位置
哪些车不用变位置呢?
因为最后的排列是连续且递增的
那么明显,能剩下来的,就是以前最长的已经排列好的子序列
也就是最长连续上升子序列
有人会说:这下就简单了,我会导弹拦截!
。。。有这么麻烦吗?
我们知道这个要求的是一个连续的序列
所以就简单了
比如说7这个数
到他为止的最长连续上升序列的长度f[7]一定是f[6]+1
所以就好办喽
代码:
#include<iostream>
#include<cstdio>
#define rii register int i
using namespace std;
int n,dp[],ans;
int main()
{
int ltt;
scanf("%d",&n);
for(rii=;i<=n;i++)
{
scanf("%d",<t);
dp[ltt]=dp[ltt-]+;
ans=max(ans,dp[ltt]);
}
cout<<n-ans;
}
CF605A Sorting Railway Cars(递推)的更多相关文章
- CF605A Sorting Railway Cars
传送门 题目大意 给出一个 1 到 n 的排列,每次操作可以将某个位置的数字移动到最前面或最后面,求将排列从小到大排序的最小操作次数 如:4 1 2 5 3 操作1:将5和3换一下变成4 1 2 3 ...
- CF605A Sorting Railway Cars 题解
To CF 这道题依题意可得最终答案为序列长度-最长子序列长度. 数据范围至 \(100000\) 用 \(O(n^2)\) 的复杂度肯定会炸. 用 \(O(nlogn)\) 的复杂度却在第 \(21 ...
- 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 ...
- 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 ...
- A. Sorting Railway Cars
A. 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 ...
随机推荐
- 设计模式之职责链模式(JAVA实现)
学习netty框架时,看到有人说netty用到了设计模式的职责链模式,学习一下职责链模式,主要参考大话设计模式. 主要场景: 小菜想要加薪,向经理提出加薪请求,经理没有权限,经理交由总监处理,总监也没 ...
- time&datetime模块
在Python中,和时间处理相关的模块有time,datatime,calendar(不常用)三个. UTCC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间, ...
- File I/O 小结
1 .I/0: input/output 2.java.io.File 3 .表示:文件或者文件夹(目录) 4. File f = new File("文件路径"); 5 .注意: ...
- Linux虚拟系统安装——Ubuntu18.04 & CentOS6.5
Linux虚拟系统安装--Ubuntu18.04 & CentOS6.5 摘要:Linux简介.虚拟系统安装.系统备份与文件介绍 1. Linux简介 (1)1968年,MIT.Bell实验室 ...
- Instance Segmentation入门总结
前一阵子好忙啊,好久没更新了.最近正好挖了新坑,来更新下.因为之前是做检测的,而目前课题顺道偏到了instance segmentation,这篇文章简单梳理一下从检测.分割结果到instance s ...
- .NET开源工作流RoadFlow-表单设计-子表
子表即明细表 从表:与主表对应在子表. 从表主键:从表的主键. 主表字段:主表中与从来关联的字段,一般为主表的主键. 与主表关联字段:从表中与主表关联的字段. 选择之后即可在下面从表字段列表中设置每个 ...
- ViewPager+fragment的使用
如图我在一个继承FragmentActivity的类中嵌套了3个fragment分别能实现3个不同的界面,默认展现第一个,在第一个的fragment中有个ViewPager在ViewPager中嵌套了 ...
- day013-流
1. 常用函数式接口 1.1 Predicate接口 有时候我们需要对某种的数据进行判断,从而得到一个boolean值结果.这时可以使用java.util.function.Predicate< ...
- UIRecorder安装与使用
继vue单元测试,将进行vue的e2e测试学习. 学习点: 安装uirecorder 用工具(UI Recorder)录制测试脚本 测试脚本的回放 本文意在安装UI Recorder,并且利用该工具进 ...
- 【Spring实战】—— 16 基于JDBC持久化的事务管理
前面讲解了基于JDBC驱动的Spring的持久化管理,本篇开始则着重介绍下与事务相关的操作. 通过本文你可以了解到: 1 Spring 事务管理的机制 2 基于JDBC持久化的事务管理 Spring的 ...