Description

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.

Sample Input

5
4 1 2 5 3

Sample Output

2

Hint

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$,因为我们只要按照大小顺序每个数都移动一次,整个序列就一定有序了,由以上结论还可以得每个数至多被移动一次。

由于操作是将数移动到序列首部和序列尾部,那么没有被移动的数会停留在序列中部,所以这些数一定是原序列的一个子序列,并且是有序序列的一个连续子段。最小化移动即是最大化不动,那么我们找到最长的这样的一个子序列就行了。

时间复杂度 $O(n)$ 。

 //It is made by Awson on 2017.10.17
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
using namespace std;
const int N = ;
void read(int &x) {
char ch = getchar(); x = ;
while (ch < '' || ch > '') ch = getchar();
while (ch >= '' && ch <= '') x = (x<<)+(x<<)+ch-, ch = getchar();
} int n, ans, a;
int f[N+]; void work() {
read(n);
for (int i = ; i <= n; i++) {
read(a); f[a] = f[a-]+;
ans = Max(ans, f[a]);
}
printf("%d\n", n-ans);
}
int main() {
work();
return ;
}

[Codeforces 606C]Sorting Railway Cars的更多相关文章

  1. CodeForces 606C Sorting Railway Cars(最长连续上升子序列)

    Description An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the n ...

  2. Codeforces 335C Sorting Railway Cars

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  3. codeforce 606C - Sorting Railway Cars

    题意:给你一串数,没个数只能往前提到首位,或则往后放末尾.问最少步骤使操作后的序列成上升序列. 思路:最长连续子序列. #include<iostream> #include<std ...

  4. CodeForces 605A Sorting Railway Cars 思维

    早起一水…… 题意看着和蓝桥杯B组的大题第二道貌似一个意思…… 不过还是有亮瞎双眼的超短代码…… 总的意思呢…… 就是最长增长子序列且增长差距为1的的…… 然后n-最大长度…… 这都怎么想的…… 希望 ...

  5. CodeForces 605A Sorting Railway Cars

    求一下最长数字连续上升的子序列长度,n-长度就是答案 O(n)可以出解,dp[i]=dp[i-1]+1,然后找到dp数组最大的值. #include<cstdio> #include< ...

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

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

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

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

随机推荐

  1. 网络1711班 C语言第四次作业批改总结

    网络1711班 C语言第四次作业批改总结 助教有话说(写在前面) 近来,有同学跟老师和助教们反映:博客作业太多太麻烦,而且对编程能力提高似乎没什么帮助?在这里我要谈一谈我的感想. 博客作业的意义? 首 ...

  2. 201621123050 《Java程序设计》第9周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 ①foreach循环 for (String e : map.keyS ...

  3. 201621123057 《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 接口,interface,implements,方法签名,has-a,Comparable,Comparator. 1.2 尝试 ...

  4. java 1.7新特性

    try( ... ){ ... } catch(xxx e){ ... } java1.7特性,叫做try-with-resource,实现了AutoCloseable接口的实例可以放在try(... ...

  5. 九、Python发送QQ邮件(SMTP)

    看了廖雪峰老师的教程: 一封电子邮件的旅程就是 发件人 -> MUA -> MTA -> MTA -> 若干个MTA -> MDA <- MUA <- 收件人 ...

  6. JS的if和switch

    var aa=parseInt(prompt("请输入你的年龄")); //定义输入 if(aa<18){ //输出小于18,返回值少年 alert("少年&quo ...

  7. pygame事件之——控制物体(飞机)的移动

    近来想用pygame做做游戏,在 xishui 大神的目光博客中学了学这东西,就上一段自己写的飞机大战的代码,主要是对键盘控制飞机的移动做了相关的优化 # -*- coding: utf-8 -*- ...

  8. iot前台开发环境:请求示例

    参考链接:http://www.cnblogs.com/keatkeat/category/872790.html 编辑->update保存 一.typescipt import { Injec ...

  9. api-gateway实践(16)【租户模块:修改api定义】通过mq通知【开发者模块:更新开发者集市】

    一.订阅关系 二.接收消息 dev模块接收更新本地集市

  10. 使用Git进行代码版本管理及协同工作

    Git简介: git是一种较为先进的代码版本管理及协同工作平台,采用分布式文件块存储: 1.  分布式: 代码保存在所有协同成员的计算机上,网速较差时依然可用:而传统的集中式代码版本管理系统则较难脱离 ...