CF605A Sorting Railway Cars
题目大意
给出一个 1 到 n 的排列,每次操作可以将某个位置的数字移动到最前面或最后面,求将排列从小到大排序的最小操作次数
如:4 1 2 5 3 操作1:将5和3换一下变成4 1 2 3 5
操作2:将1 2 3和 4换一下变成 1 2 3 4 5 在此后即完成要求。所以最小操作次数为2。
输入:
第一行 为长度 nnn ; 第二行为原来的顺序两个数之间有空格。
输出:
最小操作次数
解题思路
因为要从小到大排序,所以我们很自然能想到lis,但是也很容易举出反例。如1 2 4 5 3,如果求lis的话答案为1,实际上答案应该是2。是因为只能将数字移动到末尾或开头。但如果是连续的数字就可以不用进行操作,所以这道题应该是求出一段连续的lis,比如上面那个数据求出的话应该是3 1 2 3 。因为数字保证是一个n的排列,所以这比直接求lis要简单,直接开个桶记录上一个有没有出现,直接进行转移即可。
代码
#include<iostream>
#include<cstdio>
using namespace std;
const int MAXN = 100005;
inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
int n,a[MAXN];
int dp[MAXN],ans;
bool vis[MAXN];
int main(){
n=rd();
for(register int i=1;i<=n;i++){
a[i]=rd();
vis[a[i]]=1;
if(vis[a[i]-1]) dp[a[i]]=dp[a[i]-1]+1;
else dp[a[i]]=1;
ans=max(ans,dp[a[i]]);
}
printf("%d",n-ans);
return 0;
}
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 题解
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 ...
随机推荐
- 几道关于this的经典练习题的理解与分析
1. var num = 1; var myObject = { num: 2, add: function() { this.num = 3; (function() { console.log(t ...
- 02.MyBatis在DAO层开发使用的Mapper动态代理方式
在实际开发中,Mybatis作用于DAO层,那么Service层该如何调用Mybatis Mybatis鼓励使用Mapper动态代理的方式 Mapper接口开发方法只需要程序员编写Mapper接口(相 ...
- C++ 系列:Boost Thread 编程指南
转载自:http://www.cppblog.com/shaker/archive/2011/11/30/33583.html 作者: dozbC++ Boost Thread 编程指南0 前言1 创 ...
- eclipse变量名自动补全
对于编程人员来说,要记住大量的类名或类方法的名字,着实不是一件容易的事情.如果要IDE能够自动补全代码,那将为我们编程人员带来很大帮助. eclipse代码里面的代码提示功能默认是关闭的,只有输入“. ...
- leetcode-92-反转链表②
题目描述: 方法一: class Solution: def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode: ...
- Python-进程(1)
目录 操作系统发展史 穿孔卡片 联机批处理系统 统计批处理系统 单道 多道技术 空间上复用 时间上复用 并行与并发 进程 程序与进程 进程调度 进程的三个状态 就绪态 运行态 阻塞态 同步和异步 阻塞 ...
- 容斥原理——hdu2841
记得要开ll /* 莫比乌斯反演模板题,也可以直接算phi来做 容斥的解法 求x[1..m],在[1,n]中和其互质的数的个数即可 那么就是n-和x不互质的数个数即可 */ #include<b ...
- 19-11-10-Night
关于$Miemeng$,它死了. 大家有没有记得我在暑假里曾经写过一个著名模数? const int Mod=998224353; 现在有续集了(捂脸)(改不过题.jpg) const int Mod ...
- 反编译之paktool工具
1.官网配置mac教程https://ibotpeaches.github.io/Apktool/install/ (1)右键选择:链接储存为(命名:apktool,格式:选择所有文件): (2)下载 ...
- rocketmq 延时消息
rocketmq 的延时消息不能支持任意延时,她定义了18 个延时等级,并且我们可以指定这18 个延时等级的延时时间. 发送消息的时候只需在消息中指定 当前消息的 延时等级即可,并且这个延时消息不是 ...