#贪心#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 ...
- 【MongoDB】MongoDB原理分析、集群搭建(Docker)与简单使用
一.MongoDB 简介 MongoDB是一个基于分布式文件存储的数据库,介于关系数据库和非关系数据库之间,是非关系数据库当中功能最丰富,最像关系数据库的.其目的是为WEB应用提供可扩展的高性能数据存 ...
- 【Azure Redis 缓存】遇见Azure Redis不能创建成功的问题:至少一个资源部署操作失败,因为 Microsoft.Cache 资源提供程序未注册。
问题描述 在中国区微软云上创建Redis失败.收到的错误消息为: { "code": "DeploymentFailed", "message&quo ...
- clickhouse 安装启动报<Error> Application: DB::Exception: There is no profile 'default' in configuration file. 以及常见的错误的总结
1.启动时报错<Error> Application: DB::Exception: There is no profile 'default' in configuration file ...
- 探索图片与Base64编码的优势与局限性
一.图片和Base64编码的关系: 图片是一种常见的媒体文件格式,可以通过URL进行访问和加载. Base64编码是一种将二进制数据转换为ASCII字符的编码方式,可以将图片数据转换为字符串形式. 图 ...
- 一文讲明白Java中线程与进程、并发与与并行、同步与异步
写在开头 ok,everybody,在过去的两周内,我们大体上讲完了Java的集合,在最后我们探讨了关于HashMap线程不安全的原因,又提出了ConcurrentHashMap这个线程安全的集合解决 ...
- Java 接口的使用
1 package com.bytezreo.interfacetest; 2 3 /** 4 * 5 * @Description 接口的使用 6 * @author Bytezero·zhengl ...
- Java 求数值型数组中的最大元素 最小值 平均值 总和等 要求:随机数是 两位数
1 /* 2 * 3 * 算法考查:求数值型数组中的最大元素 最小值 平均值 总和等 4 * 要求:随机数是 两位数 5 * [10,99] 6 * 公式:(int)(Math.random()*(9 ...
- Java //遍历100以内的偶数,获取所有偶数的和,输出偶数的个数
1 //遍历100以内的偶数,获取所有偶数的和,输出偶数的个数 2 3 int i =1; 4 int sum = 0; 5 int count = 0; 6 for(i = 1;i<=100; ...
- FastStone Capture 屏幕录像软件推荐 支持录像的时候放大屏幕
FastStone Capture(屏幕截图软件)v9.7中文注册版 http://www.ucbug.com/soft/25602.html