Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 动态规划

时间:2022-09-16 08:54:38

C. Sorting Railway Cars

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://www.codeforces.com/contest/606/problem/C

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.

Sample Input

5
4 1 2 5 3

Sample Output

2

HINT

题意

给你1-n的一个排列

然后你的操作是可以把一个数放到结尾或者首部

然后问你最少多少次操作,可以将这个排列变成递增的

题解:

首先跑最长上升子序列是错的

比如 1 2 4 5 3

最长上升子序列答案跑出来是1

但实际上答案是2

所以只能跑严格只比之前大1的子序列就好了

代码:

#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std; int dp[];
int a[];
int main()
{
int n;
cin>>n;
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
dp[a[i]]=dp[a[i]-]+;
int flag = ;
for(int i=;i<=n;i++)
flag = max(flag,dp[i]);
cout<<n-flag<<endl;
}

Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 动态规划的更多相关文章

  1. Codeforces Round &num;335 &lpar;Div&period; 2&rpar; C&period; Sorting Railway Cars 连续LIS

    C. Sorting Railway Cars   An infinitely long railway has a train consisting of n cars, numbered from ...

  2. Codeforces Round &num;335 &lpar;Div&period; 2&rpar; C&period; Sorting Railway Cars

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

  3. Codeforces Round &num;335 &lpar;Div&period; 2&rpar;

    水 A - Magic Spheres 这题也卡了很久很久,关键是“至少”,所以只要判断多出来的是否比需要的多就行了. #include <bits/stdc++.h> using nam ...

  4. Codeforces Round &num;335 &lpar;Div&period; 2&rpar; C

                                                                   C. Sorting Railway Cars time limit pe ...

  5. Codeforces Round &num;556 &lpar;Div&period; 2&rpar; - D&period; Three Religions(动态规划)

    Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 3000 mSec Problem Descripti ...

  6. Codeforces Round &num;335 Sorting Railway Cars 动态规划

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

  7. Codeforces Round &num;335 &lpar;Div&period; 2&rpar; B&period; Testing Robots 水题

    B. Testing Robots Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contest/606 ...

  8. Codeforces Round &num;335 &lpar;Div&period; 1&rpar; C&period; Freelancer&&num;39&semi;s Dreams 计算几何

    C. Freelancer's Dreams Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contes ...

  9. Codeforces Round &num;335 &lpar;Div&period; 2&rpar; D&period; Lazy Student 构造

    D. Lazy Student Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/606/probl ...

随机推荐

  1. Packetbeat协议扩展开发教程 一

    原文链接:http://elasticsearch.cn/article/48 Packetbeat(https://www.elastic.co/products/beats/packetbeat) ...

  2. Python 3 学习笔记

    教程地址: http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143161198 ...

  3. 用django搭建一个简易blog系统(翻译)(一)

    Django 入门 原始网址: http://www.creativebloq.com/netmag/get-started-django-7132932 代码:https://github.com/ ...

  4. Httpclient发送json请求

    一.Httpclient发送json请求 public String RequestJsonPost(String url){    String strresponse = null;    try ...

  5. gzip解压文件报错

    #tar -xvf jdk-8u131-linux-x64.tar.gz,执行命令后报错如下: gzip: stdin: not in gzip format tar: Child returned ...

  6. Bootstrap3基础 pagination 分页按钮 简单示例

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  7. Removing Docker Containers and Images

    Removing Docker Containers and Images In a recent post aboutDocker, we looked into some things that ...

  8. DevExpress学习01——下载与安装

    记得刚接触编程时,虽然实现了功能,但用户界面十分丑陋,老师叫我们美化一下界面,不要千篇一律,当时觉得能够写出来功能就洋洋得意了,不觉得界面丑陋.后来,在程序比赛中,我接触了一种第三方控件,它可以快速实 ...

  9. UVA 1393 Highways

    https://vjudge.net/problem/UVA-1393 题意: a*b的点阵中能画多少条非水平非竖直的直线 方向‘/’ 和 方向 ‘\’ 对称 枚举直线所在矩形的i*j 直线可能重复的 ...

  10. pxe无人值守安装linux机器笔记----摘抄

    1. 基建工作 1.关闭防火墙 a)service iptables stop b)service ip6tables stop c)chkconfig iptables off d)chkconfi ...