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 ;
}

Codeforces Round #335 (Div. 2) C. Sorting Railway Cars的更多相关文章

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

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

  3. Codeforces Round #335 (Div. 2)

    水 A - Magic Spheres 这题也卡了很久很久,关键是“至少”,所以只要判断多出来的是否比需要的多就行了. #include <bits/stdc++.h> using nam ...

  4. Codeforces Round #335 (Div. 2) C

                                                                   C. Sorting Railway Cars time limit pe ...

  5. Codeforces Round #335 (Div. 2) B. Testing Robots 水题

    B. Testing Robots Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contest/606 ...

  6. Codeforces Round #335 (Div. 1) C. Freelancer's Dreams 计算几何

    C. Freelancer's Dreams Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contes ...

  7. Codeforces Round #335 (Div. 2) D. Lazy Student 构造

    D. Lazy Student Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/606/probl ...

  8. Codeforces Round #335 (Div. 2) A. Magic Spheres 水题

    A. Magic Spheres Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contest/606/ ...

  9. Codeforces Round #335 (Div. 2) D. Lazy Student 贪心+构造

    题目链接: http://codeforces.com/contest/606/problem/D D. Lazy Student time limit per test2 secondsmemory ...

随机推荐

  1. 自动清理DataGuard备机日志

    >> from zhuhaiqing.info #!/usr/bin/bash #删除DataGuard备机归档日志备份 export ORACLE_HOME=/opt/oracle/pr ...

  2. executable null\bin\winutils.exe in the Hadoop binaries.

    在windows 使用eclipse远程调用hadoop集群时抛出下面异常 executable null\bin\winutils.exe in the Hadoop binaries. 这个问题 ...

  3. iOS引用当前显示的UIAlertView

    UIAlertView在iOS里和一般的UIView不一样,有时候使用起来会有一些不便.特别要引用当前显示的UIAlertView的时候,就存在一些难度. 在iOS7以前,可以下面的代码可以解决这个问 ...

  4. 搭建redis集群遇到的坑

    搭建redis集群遇到的坑 #!/bin/bash # 作者: tuhooo # 日期: 2017.4.23 20.15 # 用途: 通过ruby脚本启动redis伪集群 if [ $2 == &qu ...

  5. centos7设置ip

    centos7不能再通过setup命令来设置ip了,但可以通过修改网卡配置文件来设置ip 在/etc/sysconfig/network-scripts目录下找到网卡配置文件,修改之前内容如下 TYP ...

  6. python 求下个月的最后一天

    [1]根据当前月求上个月.下个月的最后一天 (1)求当前月最后一天 (2)求前一个月的最后一天 (3)求下一个月的最后一天 学习示例与应用实例,代码如下: #!/usr/bin/python3 #-* ...

  7. solr查询

    1.根据字段查询: http://www.360doc.com/content/14/0306/18/203871_358295621.shtml 2.模糊查询: http://www.tuicool ...

  8. 第6章 网页解析器和BeautifulSoup第三方插件

    第一节 网页解析器简介作用:从网页中提取有价值数据的工具python有哪几种网页解析器?其实就是解析HTML页面正则表达式:模糊匹配结构化解析-DOM树:html.parserBeautiful So ...

  9. python 基础 9.12 索引

    #/usr/bin/python #-*- coding:utf-8 -*- #@Time   :2017/11/24 4:48 #@Auther :liuzhenchuan #@File   :索引 ...

  10. Linux下,部署多个Tomcat

    1.复制一个已有的tomcat,并重命名: 2.打开tomcat--conf--server.xml:[共修改3处配置] <Server port="8005" shutdo ...