博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
dp - 递推
阅读量:6654 次
发布时间:2019-06-25

本文共 2908 字,大约阅读时间需要 9 分钟。

A. Sorting Railway Cars
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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?

Input

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.

Output

Print a single integer — the minimum number of actions needed to sort the railway cars.

Examples
Input
5 4 1 2 5 3
Output
2
Input
4 4 1 3 2
Output
2
Note

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.

 

题目大意 :

  每次只能将一个汽车移动到序列最前或最后 , 问最小移动次数 。

 

思路 : 只要我找到 这串数字的 最长连续数 的个数 , 如 1 3 2 4 5 , 可以看出这段数的最长连续数的个数是 3 个 , 因此递推的状态转移方程是 dp[pre[i]] = dp[pre[ i ] - 1] + 1;

代码示例 :

 

/* * Author:  ry  * Created Time:  2017/9/28 14:25:17 * File Name: 1.cpp */#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;const int eps = 1e5+5;const double pi = acos(-1.0);#define Max(a,b) a>b?a:b#define Min(a,b) a>b?b:a#define ll long longint pre[eps];int dp[eps];int main() { int n; while (~scanf("%d", &n)){ memset(dp, 0, sizeof(dp)); for(int i = 1; i <= n; i++){ scanf("%d", pre+i); } for(int i = 1; i <= n; i++) dp[pre[i]] = dp[pre[i]-1] + 1; printf("%d\n", n - *max_element(dp+1, dp+1+n)); } return 0;}

 

/* * Author:  ry  * Created Time:  2017/9/28 14:25:17 * File Name: 1.cpp */#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;const int eps = 1e5+5;const double pi = acos(-1.0);#define Max(a,b) a>b?a:b#define Min(a,b) a>b?b:a#define ll long longint pre[eps];int dp[eps];int main() { int n; while (~scanf("%d", &n)){ memset(dp, 0, sizeof(dp)); for(int i = 1; i <= n; i++){ scanf("%d", pre+i); } for(int i = 1; i <= n; i++) dp[pre[i]] = dp[pre[i]-1] + 1; printf("%d\n", n - *max_element(dp+1, dp+1+n)); } retu

转载于:https://www.cnblogs.com/ccut-ry/p/7606590.html

你可能感兴趣的文章
Apache中使用mod_php的请求响应执行流程
查看>>
CentOS 6.3定制自动安装
查看>>
更改android源码并编译遇到的问题
查看>>
修正static控件配景色彩和文字色彩
查看>>
python 学习笔记 (核心)
查看>>
spring aop记录日志
查看>>
区块链实战超级账本视频教程|区块链视频教程
查看>>
还有人不认识通讯诈骗,短信验证码带你认识一下
查看>>
java博客集锦
查看>>
Docker(四)镜像创建
查看>>
unigui的UnimDatePicker控件使用经验
查看>>
C# 自定义堆栈进行回文检测的代码
查看>>
java为什么计算时间从1970年1月1日开始
查看>>
采用负责任的人工智能推动业务价值
查看>>
人工智能将是驱动未来城市发展的核心引擎
查看>>
网络下载图片大全
查看>>
比较实用的wamp配置多站点方法
查看>>
我的天!AI智能竟然能通过声音知道你的长相!
查看>>
用maven时出现,报错 miss 一些包,但是发现项目里已经引入了,但还是报错
查看>>
Servlet中ServletConfig和ServletContext漫谈
查看>>