C. Sorting Railway Cars
 

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 ≤ npi ≠ 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.

Sample test(s)
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.

 题意:给n长度的序列,每次能把任意位置的数移到序列头,序列尾算一个操作,问你最少经过几次操作使其有序;

题解:也就是最长连续子序列

//meek
///#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************** const int N=+;
const ll inf = 1ll<<;
const int mod= ; int ans,a[N],H[N],n;
int dp[N];
int main() {
ans=-;
scanf("%d",&n);
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
}
for(int i=;i<=n;i++) {
H[a[i]] ++;
if(H[a[i]-]) dp[a[i]]=dp[a[i]-]+;
else dp[a[i]]=;
ans=max(ans,dp[a[i]]);
}
cout<<n-ans<<endl;
return ;

代码

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

  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

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

  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 606-C:Sorting Railway Cars(LIS)

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

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

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

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

  9. 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/ ...

随机推荐

  1. Ubuntu14.04 Objective-C hello world

    1. Install GNUstep sudo apt-get install gnustep gnustep-devel 2. Write hello world program, and save ...

  2. iOS-CALayer

    iOS开发UI篇—CAlayer层的属性 一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property ...

  3. iOS进阶学习-数据库

    一.数据库管理系统 1.SQL语言概述:SQL是Structured Query Language(结构化查询语言)的缩写.SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言. 2.常见 ...

  4. iOS学习之C语言循环结构

    一.while循环    while (循环条件) {        循环体:    }    // 1.定义循环变量    int time = 1;    // 2.循环条件    while ( ...

  5. nodejs for centos配置

    mongodb http://www.cnblogs.com/zhoulf/archive/2013/01/31/2887439.html nodejs http://zhaohe162.blog.1 ...

  6. 28335 AD 转换

    #include "DSP2833x_Device.h"#include "DSP2833x_Examples.h" void Ad_init(void){ I ...

  7. 零基础学习Linux(三)linux与windows文件共享

    上次的博文零基础学习Linux(一)环境搭建中我们已经将linux环境部署完毕了,接下来我们就可以在linux上进行软件的安装和环境的配置.但在进行这些操作之前,我们还需要解决一个问题——Linux与 ...

  8. UIImageView swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  9. SQL SERVER发布与订阅 [原创]

    一.配置分发 1.配置分发服务器,注:配置发布与订阅,连接SQLSERVER必须用服务器名登录 2.配置分发 3.选择分发服务器 4.选择快照文件夹 5.设置此文件夹的读写权限为everyone 6. ...

  10. MVC5 自定义表单错误信息

    1.