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

Examples
input
Copy
5
4 1 2 5 3
output
Copy
2
input
Copy
4
4 1 3 2
output
Copy
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辆车,每辆车编号从1—n,但是顺序是无序的,有两种操作:1.将车移到最前面。2.将车移到最后面。问最少需要几次操作能让这些车的编号从小到大排序

思路:求出这些无序的编号中最长的上升子序列长度L(注意这个序列中相邻的元素相差为1),然后用n减去L就可以了(具体不知道为什么,但是随便找几组数据推一下就能出来)

实现:开两个数组a,b,a储存无序的车序号,b储存从开头到序号a[i]的上升子序列。

如第一组样例:4    1    2    5    3

a[1]=4,在4之前没有比4小1的数,所以b[a[1]]=1,即:b[4]=1;

同理b[1]=1

a[3]=2,因为能找到比2小1的数,且b[a[3]-1]=b[1]=1,即:b[2]=b[1]+1=2;

同理b[5]=b[4]+1=2;

b[3]=b[2]+1=3;

然后找出b数组中的最大值ans,n-ans即可得到正确结果

LIS的时间复杂度为O(n)

AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define ll long long
#define INF 0x3f3f3f3f
const int maxn=1e6+10;
int a[maxn];
int b[maxn];
int main(int argc, char const *argv[])
{
int n;
scanf("%d",&n);
memset(b,0,sizeof(b));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[a[i]]=b[a[i]-1]+1;
}
int ans=1;
for(int i=1;i<=n;i++)
{
ans=std::max(ans,b[a[i]]);
}
printf("%d\n",n-ans);
return 0;
}

Codeforces 606-C:Sorting Railway Cars(LIS)的更多相关文章

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

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

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

  3. CF605A Sorting Railway Cars(递推)

    题目描述 An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers ...

  4. Codeforces Gym 101142C:CodeCoder vs TopForces(搜索)

    http://codeforces.com/gym/101142/attachments 题意:每个人在TC和CF上分别有两个排名,如果有一个人在任意一个网站上大于另一个人的排名,那么这个人可以打败另 ...

  5. Codeforces Round #441 D. Sorting the Coins(模拟)

    http://codeforces.com/contest/876/problem/D 题意:题意真是难懂,就是给一串序列,第i次操作会在p[x](1<=x<=i)这些位置放上硬币,然后从 ...

  6. Educational Codeforces Round 32:E. Maximum Subsequence(Meet-in-the-middle)

    题目链接:E. Maximum Subsequence 用了一个Meet-in-the-middle的技巧,还是第一次用到这个技巧,其实这个技巧和二分很像,主要是在dfs中,如果数量减小一半可以节约很 ...

  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

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

  9. CF#335 Sorting Railway Cars

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

随机推荐

  1. MySQL中特有的函数If函数

    上面我们已经知道了case函数可以实现逻辑判断,可以是很复杂的逻辑判断,但是如果我们只想实现的是如果这个条件成立就返回A否则就返回B这样简单的逻辑如果用case的话,未免复杂了.我们可以使用if函数来 ...

  2. #C++初学记录(高精度运算)(加法)

    高精度运算 不管是int还是double亦或者long long ,这些定义变量都有数据范围的一定限制,在计算位数超过十几位的数,也就是超过他们自身的数据范围时,不能采用现有类型进行计算,只能自己通过 ...

  3. java之标记型接口Cloneable

    1.克隆用途. Cloneable和Serializable一样都是标记型接口,它们内部都没有方法和属性,implements Cloneable表示该对象能被克隆,能使用Object.clone() ...

  4. BZOJ:3832: [Poi2014]Rally

    题意: 给出$DAG$,询问删掉哪个点之后最长路径最短 思路: 我们令$f[x]$表示从最远的点到达它的距离,$g[x]$表示它能够到达最远的点的距离 那么对于$(x -> y)$一条边来说,它 ...

  5. python webdriver 测试框架-数据驱动exce驱动,不用ddt的方式

    data.xlsx: 脚本: #encoding=utf-8from selenium import webdriverimport timeimport datetimefrom openpyxl ...

  6. linux history记录格式修改

    #保存一万条命令记录 sed -i 's/^HISTSIZE=1000/HISTSIZE=10000/g' /etc/profile #在/etc/profile的文件尾部添加如下行数配置信息 ### ...

  7. 关于AutoResetEvent 和ManualResetEvent

    在公司的代码里面看到这两个类的使用了,第一次去msdn上的时候,看的一头雾水(关键是没有找对方法) http://www.cnblogs.com/qingyun163/archive/2013/01/ ...

  8. C# 版本和.NET 版本以及VS版本的对应关系

    https://en.wikipedia.org/wiki/C_Sharp_(programming_language)#Versions http://stackoverflow.com/quest ...

  9. Redis之List 列表

    Redis List 列表 Redis列表是简单的字符串列表,按照插入顺序排序.你可以添加一个元素导列表的头部(左边)或者尾部(右边) 一个列表最多可以包含 232 - 1 个元素 (42949672 ...

  10. UVa 11538 象棋中的皇后

    https://vjudge.net/problem/UVA-11538 题意: n×m的棋盘,有多少种方法放置两个相互攻击的皇后? 思路: 这两个皇后互相攻击的方式只有3种,在同一行,在同一列,或在 ...