题目描述

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",&ltt);
dp[ltt]=dp[ltt-]+;
ans=max(ans,dp[ltt]);
}
cout<<n-ans;
}

CF605A Sorting Railway Cars(递推)的更多相关文章

  1. CF605A Sorting Railway Cars

    传送门 题目大意 给出一个 1 到 n 的排列,每次操作可以将某个位置的数字移动到最前面或最后面,求将排列从小到大排序的最小操作次数 如:4 1 2 5 3 操作1:将5和3换一下变成4 1 2 3 ...

  2. CF605A Sorting Railway Cars 题解

    To CF 这道题依题意可得最终答案为序列长度-最长子序列长度. 数据范围至 \(100000\) 用 \(O(n^2)\) 的复杂度肯定会炸. 用 \(O(nlogn)\) 的复杂度却在第 \(21 ...

  3. 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 ...

  4. CF#335 Sorting Railway Cars

    Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. 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 ...

  6. 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 ...

  7. A. Sorting Railway Cars

    A. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. 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 ...

  9. Codeforces 335C Sorting Railway Cars

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

随机推荐

  1. linq(查询)

    1.改变数据库某一字段的属性 db.tableName.ToList().ForEach(x => x.State = false); 2.排序 db.tableName..toList().O ...

  2. (五)html部分标签元素补充

    html标签元素十分的多,一次性掌握全部,那是不可能的,在后续的学习中,会不断补充标签元素... 1.行元素和块元素 行标签元素即是标签元素根据内容大小进行自适应,而不是占据一整行. 如<spa ...

  3. 什么时候修改class

    点击按钮class发生改变 <html> <head> <meta http-equiv="Content-Type" content="t ...

  4. git如何进行远程分支切换

    git如何进行远程分支切换 git上查看远程分支命令: git branch -a  例如: 然后我想切换到daily/1.0.0远程分支:前提是必须要创建一个本地分支,并让它和远程分支进行关联,gi ...

  5. 【阿里云产品公测】小白对OTS两点小建议

    作者:阿里云用户荷包蛋 我是大一的新生,作为一个爱技术爱学习爱折腾的熊孩子,我在暑假申请了ECS,学到了很多东西.现在阿里巴巴又开放了很多免费测试,我抱着学习和围观的心态申请了测试,其中有OTS这个高 ...

  6. 关于程序中使用servlet-api.jar和jsp-api.jar与服务器lib包jar包冲突的问题

    问题描述:         程序中使用到javax.servlet.http.HttpServletRequest等类内容,然而这些类是依赖于tomcat容器lib包下的jar包,工程中导入这两个ja ...

  7. SQL 创建自定义方法,匹配正则

    C#代码 using System.Text.RegularExpressions; public class FunctionRegex { [Microsoft.SqlServer.Server. ...

  8. 2.GlusterFS 安装配置

    2.1 GlusterFS 安装前的准备 服务器规划:(vmware 实验) 操作系统 IP 主机名 数据盘(2 块) CentOS 6.8 x86_64 10.1.0.151 mystorage1 ...

  9. 描边时消除锯齿SetSmoothingMode

    SmoothingModeAntiAlias 指定消除锯齿的呈现. SmoothingModeDefault 指定默认模式. SmoothingModeHighQuality 指定高质量.低速度呈现. ...

  10. 第一次使用Git

    这次的作业是关于GIT的,一开始我并不知道GIT是啥,百度了一下才知道Git是一个开源的分布式版本控制系统,可以有效.高速的处理从很小到非常大的项目版本管理. Git 是 Linus Torvalds ...