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

output

input

output

题意:

一条无限长的铁路有一列由n个车厢组成的列车,编号从1到n(所有车辆的编号都是不同的),并按任意顺序排列。 小北极熊希望按照编号越来越大的顺序排列火车车厢。他一次可以让其中一车厢从它原本的地方消失,并将其传送到火车的起点,或者到达火车的尽头。 小北极熊为了对车厢进行排序需要执行的最少移动次数是多少?

思路:

求最长子序列,但是是数值连续的,比如1 3 5 7 2 4 6 8,最长数值连续上升序列为2,即1 2或者3 4或者5 6或者7 8,一定要数值连续,然后用总数减去这个数就是最后所求答案。

这个题用哈希的方法解决,特别巧妙。

代码:

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <stdio.h>
#define IO ios::sync_with_stdio(false);\
cin.tie();\
cout.tie();
using namespace std;
int b[];
int main()
{
IO;
int n,a;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a);
b[a]=b[a-]+;//核心代码
}
sort(b+,b+n+);//只是为了找最大值
printf("%d\n",n-b[n]);
}

附上另一个代码,实际两个代码思路相同~

#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <stdio.h>
#define IO ios::sync_with_stdio(false);\
cin.tie();\
cout.tie();
using namespace std;
int a[];
int main()
{
int n;
int i,j,k,l,max;
while(~scanf("%d",&n))
{
for(i = ; i <= n; i++)
{
scanf("%d",&k);
a[k] = i;
}
max = ;
for(i = ; i <= n-; i+=l)
{
l=;
for(j = i+; j <= n; j++)
{
if(a[j]>a[j-])
l++;
else
break;
}
if(l > max)
max = l;
}
printf("%d\n",n-max);
}
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

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

  5. CodeForces 605A Sorting Railway Cars 思维

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

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

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

  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. Islands and Bridges(POJ2288+状压dp+Hamilton 回路)

    题目链接:http://poj.org/problem?id=2288 题目: 题意:求Hamilton 路径权值的最大值,且求出有多少条权值这么大的Hamilton路径. 思路:状压dp,dp[i] ...

  2. Verilog笔记.4.inout端口

    inout是一个双向端口,实现为使用三态门,第三态为高阻态‘z’. 在实际电路中高阻态意味着响应的管脚悬空.断开. 当三态门的控制信号为真时,三态门选通,作输出端口使用:控制信号为假时,三态门是高阻态 ...

  3. 【zTree】zTree的3.5.26静态树与动态树(实用)

    1.静态树: 目录结构:(css与js为下载的原文件夹)

  4. (4)剑指Offer之链表相关编程题

    一 链表中倒数第k个节点 题目描述: 输入一个链表,输出该链表中倒数第k个结点 问题分析: 一句话概括: 两个指针一个指针p1先开始跑,指针p1跑到k-1个节点后,另一个节点p2开始跑,当p1跑到最后 ...

  5. 初窥ThinkPHP

    MVC全称(Model View Controller) Model:模型(可以理解位数据库操作模型) View:视图(视图显示) Controller:(控制器) 简单的说框架就是一个类的集合.集合 ...

  6. python算法之近似熵、互近似熵算法

    理论基础 近似熵? 定义:近似熵是一个随机复杂度,反应序列相邻的m个点所连成折线段的模式的互相近似的概率与由m+1个点所连成的折线段的模式相互近似的概率之差. 作用:用来描述复杂系统的不规则性,越是不 ...

  7. MongoDB之conf配置文件详解(五)

    详细看一下mongodb配置文件. mongodb.conf # mongodb.conf # 数据库文件位置 dbpath=/var/lib/mongodb #日志文件的路径 logpath=/va ...

  8. ThoughtWorks代码挑战——FizzBuzzWhizz游戏 通用高速版(C/C++ & C#)

    最早看到这个题目是从@ 程序媛想事儿(Alexia) 的 最难面试的IT公司之ThoughtWorks代码挑战——FizzBuzzWhizz游戏 开始的,然后这几天陆陆续续有N个小伙伴发表了自己的文章 ...

  9. 20180830 安装git时报错,

    安装:https://blog.csdn.net/u013256816/article/details/54743470 解决问题:https://blog.csdn.net/daojibruce/a ...

  10. SSL与HTTPS,HTTP有什么联系

    有人问:http和https有什么区别? HTTP,全称"Hyper Text Transfer Protocol",是从浏览器访问网站时使用的默认协议.由于浏览器到网站之间的数据 ...