#贪心#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 ...
随机推荐
- win32-封装BeginPaint
Graphics* StartPaint(HWND win, HDC* hdc, PAINTSTRUCT* ps) { *hdc = BeginPaint(win, ps); return new G ...
- SpringBoot的自动装配原理及应用
什么是SpringBoot自动装配 所谓的"SpringBoot自动装配"就是指:通过注解和一些简单的配置就能将某些组件载入Spring容器环境中,便于使用. 比如,很多sprin ...
- mysql进阶优化2---day41
# ### part1 索引树高度 # 1.表的数据量 数据量越大,树的高度就会变高,理论上三层索引树的高度最为理想,可以支持百万级别的数据量 解决:可以使用分表(横切,竖切),分库,增加缓存,解决数 ...
- 【Azure Compute Gallery】使用 Python 代码从 Azure Compute Gallery 复制 Image-Version
问题描述 Azure Compute Gallery 可以帮助围绕 Azure 资源(例如映像和应用程序)生成结构和组织,并且支持全局复制. 如果想通过Python代码实现 Image-Version ...
- Educational Codeforces Round 145 (Rated for Div. 2)C. Sum on Subarrays(构造)
很意思的一道构造题 题意:给一个\(n.k\),让构造长度为n的数组满足,子数组为整数的个数为k个,负数的为\(k-(n+1)* n/2\),每个数的范围为\([-1000,1000]\) 这种构造题 ...
- 摆脱鼠标系列 - vscode 软件 最大化快捷键 - win + ↑
摆脱鼠标系列 - vscode 软件 最大化快捷键 - win + ↑ vscode默认打开不是最大化,所以按 win + 上箭头 使其最大化 不想按 F11 那个不太方便,左上角就没有项目名称了 优 ...
- vscode 智能提示 函数提示右侧有所在目录 Show Inline Details
vscode 智能提示 函数提示右侧有所在目录 Show Inline Details 有目录提示的 没有目录提示的 默认是有目录提示,我那个配置单啊~ "editor.suggest.sh ...
- node开发命令行脚本 / commander
1. 脚本第一行添加 #!/usr/bin/env node // index.js #!/usr/bin/env node console.log('hello world') 2. package ...
- PV的回收策略、访问策略和状态
PersistentVolume(PV)的回收策略.访问策略和状态是Kubernetes存储管理中的重要概念. 回收策略 Retain:当PV的回收策略设置为Retain时,即使对应的Persiste ...
- c语言运算符优先级实例解析
壹: 对于优先级:算术运算符 > 关系运算符 > 逻辑运算符 > 赋值运算符.逻辑运算符中"逻辑非 !"除外.这是程序员总结出来的最快的学习方式. 可在实战 ...