题目描述

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. 《CSS实现单行、多行文本溢出显示省略号》

    如果实现单行文本的溢出显示省略号同学们应该都知道用text-overflow:ellipsis属性来,当然还需要加宽度width属来兼容部分浏览. 实现方式: overflow: hidden; te ...

  2. 常用css属性记录

    CSS常用属性: 字体属性:(font)大小 font-size: x-large;(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD样式 font-style: ...

  3. 文件上传fileupload文件接收

    form表单提交数据到servlet后,使用fileupload进行接收. fileupload 是由 apache 的 commons 组件提供的上传组件.它最主要的工作就是帮我们解析 reques ...

  4. io饥饿

    看书,在书上看到一句话,防止io饥饿,google了一下,也没有找到相关的解释,究竟什么是io饥饿.

  5. eclipse插件svn和客户端工具TortoiseSvn的版本对应关系

    如果同时使用这两个软件,一定要保证版本的对应关系: 插件svn1.4.x对应TortoiseSvn 1.5.x 插件svn1.6.x对应TortoiseSvn 1.6.x 插件svn1.8.x对应To ...

  6. docker 部署 nginx+php+mysql

    系统:centos7 使用root账户 开放80端口 firewall-cmd --zone=public --add-port=80/tcp --permanent 安装docker yum ins ...

  7. 生产环境rac无法启动

    节点二crs无法启动,查看启动日志:ohasd.log位置在/u01/app/11.2.0/grid/log/host01/ohasd/ohasd.log另外root.sh的log在rootcrs_X ...

  8. IOS 摇一摇的方法

    ● 监控摇一摇的方法 ● 方法1:通过分析加速计数据来判断是否进行了摇一摇操作(比较复杂) ● 方法2:iOS自带的Shake监控API(非常简单) ● 判断摇一摇的步骤:实现3个摇一摇监听方法 ● ...

  9. Android(java)学习笔记58:Android 英文朗诵

    1. 首先,我先把代码放到下面: package com.himi.speaker; import java.util.Locale; import android.app.Activity; imp ...

  10. SQL SERVER 2012断日志

    有一个SQL2012库的日志达到了100G左右,平时开发人员根本没有做过事务日志备份,而磁盘空间已经快满了.所以,只能截断它.但是,由于从2K8以后,SQL SERVER好像不再提供 truncate ...