#贪心#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 ...
随机推荐
- pinia
Pinia学习 Vue3中 使用 官网:https://pinia.web3doc.top/introduction.html 安装 yarn add pinia # 或者使用 npm npm ins ...
- 一个Git Commit Message模板
一个统一的commit消息模板可以约束团队成员使用一致的方式提交变更信息,这样也方便集成工具进行合规检查. 通常来讲,commit信息应该包含如下内容: <type>(<scope& ...
- flutter——android报错Manifest merger failed : Attribute application@allowBackup value=(false)
与这个https://www.cnblogs.com/MaiJiangDou/p/13848658.html 报错类似. 报错: Manifest merger failed : Attribute ...
- 默认形参和关键字实参,收集参数,命名关键字参数,return自定义返回,全局变量和局部变量,函数名的使用---day10
1.函数定义处(默认形参在函数的定义) 1.1.函数的调用处(关键字实参在函数的调用处) 2.收集参数 (1)收集参数: (1) 普通收集参数 在参数的前面加一个*,代表的是普通收集参数 作用:收集多 ...
- Q查询的高级用法
示例:如前端需要通过下拉框选择需要通过什么过滤字段来查询输入的关键字,后端如何使用Q查询过滤包含输入的关键字呢? def customers(request): search_field = requ ...
- windows下的批处理bat文件和Linux下的shell文件的含义
原文:https://www.cnblogs.com/caiguodong/p/10308255.html shell(Linux.Solaris) bat(windows) 含义 # rem 注释行 ...
- 【LeetCode动态规划#16】矩阵的最小路径和、三角形的最小路径和
矩阵的最小路径和 给定一个包含非负整数的 *m* x *n* 网格 grid ,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:一个机器人每次只能向下或者向右移动一步. 示例 1 ...
- 【LeetCode哈希表#5】四数相加II(map)
四数相加II 力扣题目链接(opens new window) 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + ...
- Kubernetes:Pod 端口映射
本文为作者的 Kubernetes 系列电子书的一部分,电子书已经开源,欢迎关注,电子书浏览地址: https://k8s.whuanle.cn[适合国内访问] https://ek8s.whuanl ...
- NebulaGraph v3.3.0 发布:支持子图过滤、和大量性能优化
NebulaGraph 3.3.0 支持了 GET SUBGRAPH 和 GetNeighbors 的点过滤.引入了大量性能优化,同时,开始对无 tag 顶点的支持默认关闭. 优化 优化了 k-hop ...