题目

一个长度为 \(n\) 的排列,每次可以将一个数移至开头或者结尾,问最少多少次使其升序排列


分析

让数字连续的情况尽量多才能让移出来的次数尽量少,

找到最长的数字连续段,若其长度为 \(len\),那么答案为 \(n-len\)


代码

#include <cstdio>
#include <cctype>
using namespace std;
int n,ans,las[100011],f[100011];
int iut(){
int ans=0; char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=ans*10+c-48,c=getchar();
return ans;
}
int max(int a,int b){return a>b?a:b;}
int main(){
n=iut();
for (int i=1;i<=n;++i){
int x=iut();
if (las[x-1]) f[i]=f[las[x-1]]+1;
else f[i]=1;
ans=max(ans,f[i]),las[x]=i;
}
return !printf("%d",n-ans);
}

#贪心#CF605A Sorting Railway Cars的更多相关文章

  1. CF605A Sorting Railway Cars(递推)

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

  2. CF605A Sorting Railway Cars

    传送门 题目大意 给出一个 1 到 n 的排列,每次操作可以将某个位置的数字移动到最前面或最后面,求将排列从小到大排序的最小操作次数 如:4 1 2 5 3 操作1:将5和3换一下变成4 1 2 3 ...

  3. CF605A Sorting Railway Cars 题解

    To CF 这道题依题意可得最终答案为序列长度-最长子序列长度. 数据范围至 \(100000\) 用 \(O(n^2)\) 的复杂度肯定会炸. 用 \(O(nlogn)\) 的复杂度却在第 \(21 ...

  4. CF#335 Sorting Railway Cars

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

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

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

  7. A. Sorting Railway Cars

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

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

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

  10. Codeforces 335C Sorting Railway Cars

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

随机推荐

  1. Go语言并发编程(3):sync包介绍和使用(上)-Mutex,RWMutex,WaitGroup,sync.Map

    一.sync 包简介 在并发编程中,为了解决竞争条件问题,Go 语言提供了 sync 标准包,它提供了基本的同步原语,例如互斥锁.读写锁等. sync 包使用建议: 除了 Once 和 WaitGro ...

  2. CentOS8安装Docker报错问题解决

    问题描述 CentOS版本:8.5.2111. # cat /etc/redhat-release CentOS Linux release 8.5.2111 安装准备: # 安装所需软件包 sudo ...

  3. Java的SPI机制实践

    Java SPI机制概述 先给出结论:"Java的SPI是一种服务发现机制,用于约定接口和动态发现实现类,体现了分层解耦的思想". Java的SPI机制常用于框架扩展或组件替换,最 ...

  4. 2021-07-21 vue插槽

    说明 为什么要有插槽? 是为了方便优雅地在父组件中向子组件传递向子组件传递dom结构. 代码处理 子组件 该子组件的组件名为ChildComponent: <template> <d ...

  5. http.Handler接口

    // 示例 // net/http package http type Handler interface{ ServeHTTP(w ResponseWriter, r *Request) } fun ...

  6. 如何在 WindowManager.addView 中使用 Jetpack Compose

    如何在 WindowManager.addView 中使用 Jetpack Compose 一.引出问题 Android 开发中,很常见的一个场景,通过 WindowManager.addView() ...

  7. Taurus.MVC WebMVC 入门开发教程3:数据绑定Model

    前言: 在这篇 Taurus.MVC WebMVC 入门开发教程的第三篇文章中, 我们将重点介绍如何进行数据绑定操作,还会学习如何使用 ${属性名称} CMS 语法来绑定页面上的元素与 Model 中 ...

  8. 【Azure Developer】使用 Microsoft Graph API查看用户状态和登录记录

    问题描述 通过Microsoft Graph的API如何来查看用户信息和登录记录呢? 问题解答 第一步:需要一个授权Token 比如一个拥有查看用户权限的Azure账号,通过Azure CLI 命令获 ...

  9. 影刀rpa:第二个项目学习心得

    教程有说到元素的关联操作,教程说自上而下的html路径,一时之间没弄清楚,索性就去看了下网页的html源码,才弄清楚到底是咋回事: 我是先选中了列表子元素的价格字段,选择两次以后就能选择到所有列表子元 ...

  10. windows编译ZLMediaKit流媒体服务webrtc

    环境说明 ZLMediaKit编译需要的软件 visual studio 2022 cmake 3.29.0-rc2 OpenSSL 1.1.1w(不想踩坑的话安装这个版本) libsrtp 2.6. ...