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

题意:给出1-n的n个数,各不相同,每一步可以将任意一个取出,放在开头或者结尾,问把这个序列变回1-n,要多少步。

分析:显然,每个数最多取出一次,否则没有意义。

这样的话,我们可以将取出和放下(放在开头或者结尾)分开考虑。

取出之后剩下的东西一定是一个连续的上升子序列,当这个序列最长时,答案最优。

因为可以调整取出顺序,放回当然按大小顺序放回(即按大小顺序取出)就好。

注意一定是连续的上升子序列,这里的连续指的是数值上的连续。

 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = ;
int n, arr[N];
int cnt[N]; inline void Input()
{
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%d", &arr[i]);
} inline void Solve()
{
for(int i = ; i <= n; i++)
cnt[arr[i]] = cnt[arr[i] - ] + ;
int ans = ;
for(int i = ; i <= n; i++)
ans = max(ans, cnt[i]);
printf("%d\n", n - ans);
} int main()
{
freopen("a.in", "r", stdin);
Input();
Solve();
return ;
}

CF#335 Sorting Railway Cars的更多相关文章

  1. Codeforces Round #335 Sorting Railway Cars 动态规划

    题目链接: http://www.codeforces.com/contest/606/problem/C 一道dp问题,我们可以考虑什么情况下移动,才能移动最少.很明显,除去需要移动的车,剩下的车, ...

  2. cf 605A Sorting Railway Cars 贪心 简单题

    其实就是求总长度 - 一个最长“连续”自序列的长度 最长“连续”自序列即一个最长的lis,并且这个lis的值刚好是连续的,比如4,5,6... 遍历一遍,贪心就是了 遍历到第i个时,此时值为a[i], ...

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

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

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

  6. A. Sorting Railway Cars

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

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

  8. Codeforces 335C Sorting Railway Cars

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

  9. 【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/E Description An infinitely lon ...

随机推荐

  1. Git命令之上传与同步

    操作步骤,可参考:http://blog.csdn.net/chenyufeng1991/article/details/47299461. 1.在本地仓库中,即项目目录下创建文件,如: 2.查看当前 ...

  2. 51nod1049(计算最大子段和)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1049 题意:又是仲文题诶- 思路:暴力会超时,又好像没什么专门 ...

  3. codevs 1080 线段树练习

    链接:http://codevs.cn/problem/1080/ 先用树状数组水一发,再用线段树水一发 树状数组代码:84ms #include<cstdio> #include< ...

  4. windows  远程桌面命令 mstsc

    win+R------>mstsc: 弹出: 目标机必开远程

  5. jquery.query.js 插件(示例及简单应用)

    帮助文档 var url = location.search; > "?action=view&section=info&id=123&debug&te ...

  6. gbdt可视化

    gbdt的最大优点,和决策树一样,高度可解释,最喜欢的分类模型:) #!/usr/bin/env python #coding=gbk # ============================== ...

  7. 你可能不知道的java、python、JavaScript以及jquary循环语句的区别

    一.概述 java循环语句分为四种形式,分别是 while, do/while, for, foreach: python中循环语句有两种,while,for: JavaScript中循环语句有四种, ...

  8. JqueryDemoTools-用于整理jQueryDemo 分类: C# 公共资源 2014-12-02 16:50 224人阅读 评论(1) 收藏

    应用背景: 在学习js时,为了熟悉某个功能,或使用某个插件,往往需要写一个Demo来测试:一些好的Demo也可以整理积累下来,方便以后查阅: 写了一个编写jQuery Demo的辅助工具.界面很简单, ...

  9. 解决linux下unzip中文有乱码的问题

    xxx.zip 中有中文的文件,在linux下unzip就会有乱码. 解决办法:安装7zip 去http://sourceforge.net/projects/p7zip/files/latest/d ...

  10. ubuntu初始化root帐号密码

    Ubuntu Kylin 14.04的安装过程中并没有提供设置root密码的过程,取而代之的是自定义的帐号. 如果我们需要使用到root帐号或者root权限,则需要重新设置root帐号的密码. 设置方 ...