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. golang的并发

    Golang的并发涉及二个概念: goroutine channel goroutine由关键字go创建. channel由关键字chan定义 channel的理解稍难点, 最简单地, 你把它当成Un ...

  2. Mysql海量数据存储和解决方案之一—分布式DB方案

    1)  分布式DB水平切分中用到的主要关键技术:分库,分表,M-S,集群,负载均衡 2) 需求分析:一个大型互联网应用每天几十亿的PV对DB造成了相当高的负载,对系统的稳定性的扩展性带来极大挑战. 3 ...

  3. java提高篇-----详解java的四舍五入与保留位

    转载:http://blog.csdn.net/chenssy/article/details/12719811 四舍五入是我们小学的数学问题,这个问题对于我们程序猿来说就类似于1到10的加减乘除那么 ...

  4. android NDK开发环境搭建

    android NDK开发环境搭建 2012-05-14 00:13:58 分类: 嵌入式 基于 Android NDK 的学习之旅-----环境搭建 工欲善其事必先利其器 , 下面介绍下 Eclip ...

  5. curl库 c语言的curl 编程

    c语言的curl 编程 [Linux@centos-64-min exercise]# gcc -Wall -o curltest curltest.c /tmp/ccosVANi.o: In fun ...

  6. 20145120 《Java程序设计》实验三实验报告

    20145120 <Java程序设计>实验三实验报告 实验名称:敏捷开发与XP实践 实验目的与要求: XP基础 XP核心实践 相关工具 (一)敏捷开发与XP 极限编程(eXtreme Pr ...

  7. BICEP单元测试计划-四则运算-测试

    一.6个值得测试的具体部位,他们能够提高你的测试技巧 Right-结果是否正确? B-是否所有的边界条件都是正确的? I-能查一下反向关联吗? C-能用其他手段交叉检查一下结果吗? E-你是否可以强制 ...

  8. Map、Set、List、Queue、Stack的特点与用法

    Collection          接口的接口   对象的集合 ├ List                   子接口      按进入先后有序保存   可重复 │├ LinkedList    ...

  9. 6、android开发中遇到的bug整理

    1.使用actionProvider时出现的问题 bug复现: 解决方案: //import android.support.v4.view.ActionProvider; import androi ...

  10. 【python】文件的输入和输出

    1.os模块 2.os.path 模块 3.实例 1. os模块 对文件系统的访问大多通过python的os模块实现,其中os 模块负责大部分的文件系统操作,包括删除/重命名文件,遍历目录树,管理文件 ...