C. 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,2,3,4,。。。。n;

   所以我本来想求最长公共子序列,发现时间复杂度太大;

   后来想因为一定上升只要求最长连续上升即可;

#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define esp 1e-13
const int N=1e5+,M=1e6+,inf=1e9+,mod=;
int a[N];
int dp[N];
int flag[N];
int main()
{
int x,y,z,i,t;
scanf("%d",&x);
for(i=;i<=x;i++)
scanf("%d",&y),flag[y]=i;
dp[]=;
for(i=;i<=x;i++)
{
dp[i]=;
if(flag[i]>flag[i-])
dp[i]=dp[i-]+;
}
int ans=;
for(i=;i<=x;i++)
ans=max(ans,dp[i]);
printf("%d\n",x-ans);
return ;
}

最新文章

  1. WEB API异常处理
  2. cherrypy应用探究
  3. express+gulp构建项目(三)gulp任务
  4. ASP.NET 表单验证实现浅析
  5. mysql 前缀索引
  6. android edittext 点击回车会响应两次的解决方案
  7. SGU 164.Airline(结论题)
  8. 使用symbolicatecrash分析crash文件
  9. 【POJ1581】A Contesting Decision(简单模拟)
  10. java学习:AWT组件和事件处理的笔记(1)--菜单条,菜单,菜单项
  11. 使用hibernate 分表做增删改查
  12. cocos2d-x 多点触控实现缩放及相关问题的解决方法
  13. PHP-语法(www.w3school.com.cn/php)
  14. RockMongo 安装
  15. scroll、scrollBy和 scrollTo三种方法定位滚动条位置
  16. NOIP2017day1游记
  17. django——中间件
  18. 【EMV L2】DDA标准动态数据认证处理流程
  19. Scrum Meeting 博客目录
  20. Windows抓屏技术

热门文章

  1. Java基础IO流
  2. html 绘图阴影和透明度
  3. Hadoop学习笔记(二)——zookeeper使用和分析
  4. Windows找出占用端口的进程
  5. 基于Linux整形时间的常用计算思路
  6. poj2115[扩展欧几里德]
  7. visual studio code (vscode)像 sublime text 的 ctrl+d 一样多光标选中
  8. [转]linux terminal中使用proxy
  9. VS2015 C#6.0 中的那些新特性(转自http://www.cnblogs.com/henryzhu/p/new-feature-in-csharp-6.html)
  10. 【python】-- web开发之jQuery