#贪心#CF605A Sorting Railway Cars
题目
一个长度为 \(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的更多相关文章
- CF605A Sorting Railway Cars(递推)
题目描述 An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers ...
- CF605A Sorting Railway Cars
传送门 题目大意 给出一个 1 到 n 的排列,每次操作可以将某个位置的数字移动到最前面或最后面,求将排列从小到大排序的最小操作次数 如:4 1 2 5 3 操作1:将5和3换一下变成4 1 2 3 ...
- CF605A Sorting Railway Cars 题解
To CF 这道题依题意可得最终答案为序列长度-最长子序列长度. 数据范围至 \(100000\) 用 \(O(n^2)\) 的复杂度肯定会炸. 用 \(O(nlogn)\) 的复杂度却在第 \(21 ...
- CF#335 Sorting Railway Cars
Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 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 ...
- 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 ...
- A. Sorting Railway Cars
A. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 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 ...
- 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 ...
- Codeforces 335C Sorting Railway Cars
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
随机推荐
- [BUUCTF][Web][极客大挑战 2019]Secret File 1
打开靶机对应的url 右键查看网页源代码,查看到一个访问路径 /Archive_room.php 构造url访问一下 http://3bfaebad-fdfa-4226-ae0a-551f0228be ...
- 红胖子(红模仿)的博文大全:开发技术集合大版本更新v4.0.0
<红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术.树莓派.三维.OpenCV.OpenGL.ffmpeg.OSG.单片机.软硬结合等等)持续更新中...>大版本更新,更新后版本 ...
- Gitlab 16.9.0 用Access Token注册Gitlab Runner
升级到当前最新版Gitlab之后 在"管理中心"的"CI/CD"--"Runners"下,提示以前的那种注册令牌的方式已经过时了. 点击右上 ...
- m1芯片mac安装homebrew
安装 ARM 版 Homebrew ARM版Homebrew最终被安装在/opt/homebrew路径下. 直接执行: /bin/bash -c "$(curl -fsSL https:// ...
- 【Azure 应用服务】Azure Function 部署槽交换时,一不小心把预生产槽上的配置参数交换到生产槽上,引发生产错误
问题描述 部署Function代码先到预生产槽中,进行测试后通过交换方式,把预生产槽中的代码交换到生产槽上,因为在预生产槽中的设置参数值与生产槽有不同,但是在交换的时候,没有仔细检查.导致在交换的时候 ...
- 【Azure 应用服务】由Web App“无法连接数据库”而逐步分析到解析内网地址的办法(SQL和Redis开启private endpoint,只能通过内网访问,无法从公网访问的情况下)
问题描述 在Azure上创建的数据库,单独通过SQL的连接工具是可以访问,但在Web App却无法访问,错误信息为: { "timestamp": "2021-05-20 ...
- Program type already present: com.xxx
该错误是由于工程中存在着相同的类导致(包名与类名都相同),有可能是不同的依赖中有着相同的类,全局搜索该类便可得知
- 2、mysql存储引擎
存储引擎 1 存储引擎概述 和大多数的数据库不同, MySQL中有一个存储引擎的概念, 针对不同的存储需求可以选择最优的存储引擎. 存储引擎就是存储数据,建立索引,更新查询数据等等技术的实现方式 .存 ...
- weekToDo - 一个本地todo软件 - 软件推荐 先用着试试
https://weektodo.me/ https://github.com/Zuntek/WeekToDoWeb/releases/download/v1.7.0/WeekToDo-Setup-1 ...
- WPF之XAML语法
目录 树形结构 对象属性赋值语法 使用标签的Attribute 使用属性元素 扩展:标记扩展(Markup Extensions) 扩展:使用TypeConverter 类映射Atribute与Pro ...