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 ...
随机推荐
- mockito 初识
转载:http://blog.csdn.net/zhoudaxia/article/details/33056093 在平时的开发工作中,经常会碰到开发进度不一致,导致你要调用的接口还没好,此时又需要 ...
- css 字体样式设置
css字体样式(Font Style),属性 时间:2014-05-08 21:49 来源:我爱学习网 | 作者:我爱学习网 | 本文已影响 68353 人 css字体样式(Font Style) ...
- Matlab GUI选项卡
1.在这个网址下载一个工具包,里面应该有四个文件:tabselectionfcp.p.tabselectionfcn.m.tabpanel.p和tabpanel.m,显然代码用.p格式进行加密了. 2 ...
- matlab练习程序(广度优先搜索BFS、深度优先搜索DFS)
如此经典的算法竟一直没有单独的实现过,真是遗憾啊. 广度优先搜索在过去实现的二值图像连通区域标记和prim最小生成树算法时已经无意识的用到了,深度优先搜索倒是没用过. 这次单独的将两个算法实现出来,因 ...
- 把IDENTITY_INSERT 设置为 ON ,还不能插入数据问题
IDENTITY_INSERT 为 ON 时 , 必须把需要插入的列名列出来 不然报错 正确例子: SET IDENTITY_INSERT table(表名) ONinsert into table ...
- Linux下Vue项目搭建karma测试框架
前提:vue项目已创建,node.js.npm已安装 1.全局安装karma脚手架 karma-cli [貌似可以不安装] #npm i -g karma-cli 2.转到Vue项目目录,项目下安装 ...
- PS软件怎么把视频转成gif动态图?
PS软件怎么把视频转成gif动态图?Adobe PhotoShop软件的最新版本是可以编辑视频的,并且可以将视频转换为GIF动态图,使用也很简单,下面分享ps制作gif动态图的教程,需要的朋友可以参考 ...
- Docker入门系列之一:在一个Docker容器里运行指定的web应用
实现题目描述的这个需求有很多种办法,作为入门,让我们从最简单的办法开始. 首先使用命令docker ps确保当前没有正在运行的Docker实例. 运行命令docker run -it nginx: 然 ...
- Python变量和数据类型(入门2)
转载请标明出处: http://www.cnblogs.com/why168888/p/6400809.html 本文出自:[Edwin博客园] Python变量和数据类型 一.整数 int = 20 ...
- LeetCodeOJ刷题之12【Integer to Roman】
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...