传送门

题目大意

给出一个 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的更多相关文章

  1. CF605A Sorting Railway Cars(递推)

    题目描述 An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers ...

  2. CF605A Sorting Railway Cars 题解

    To CF 这道题依题意可得最终答案为序列长度-最长子序列长度. 数据范围至 \(100000\) 用 \(O(n^2)\) 的复杂度肯定会炸. 用 \(O(nlogn)\) 的复杂度却在第 \(21 ...

  3. CF#335 Sorting Railway Cars

    Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  4. 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 ...

  5. 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 ...

  6. A. Sorting Railway Cars

    A. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  7. 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 ...

  8. 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 ...

  9. Codeforces 335C Sorting Railway Cars

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

随机推荐

  1. Codeforces 548E Mike ans Foam (与质数相关的容斥多半会用到莫比乌斯函数)

    题面 链接:CF548E Description Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a sp ...

  2. linux yum 安装 卸载

    安装一个软件时 yum -y install httpd 安装多个相类似的软件时 yum -y install httpd* 安装多个非类似软件时 yum -y install httpd php p ...

  3. Taro踩坑记录一: swiper组件pagestate定制,swiperChange中setState导致组件不能滚动。

    import Taro, { Component } from '@tarojs/taro'; import { Swiper, SwiperItem, Image, View } from '@ta ...

  4. Java-MyBatis-MyBatis3-XML映射文件:自动映射

    ylbtech-Java-MyBatis-MyBatis3-XML映射文件:自动映射 1.返回顶部 1. 自动映射 正如你在前面一节看到的,在简单的场景下,MyBatis 可以为你自动映射查询结果.但 ...

  5. Android之相关术语

    Dalvik: Android特有的虚拟机,和JVM不同,Dalvik虚拟机非常适合在移动终端上使用! AVD: (android virtual machine):安卓虚拟设备,就是安卓的模拟器 A ...

  6. PHP实现图片的汉明码提取与降维

    作者感言:数学不好,遇到算法问题分分钟狗带,毫无转寰的余地-_-||| 最近心血来潮,看了相似图片的搜索,最最最初级的方法即提取汉明码,之后匹配汉明距离.当然,在数以亿计的汉明码中,要筛出需要的图片, ...

  7. Solrj demo

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  8. python3-常用模块之os

    os模块,os是和操作系统交互的模块 os.getcwd() :获取当前工作目录,即当前python脚本工作的目录路径,如果是命令行模式下,同样表示当前目录下 os.listdir(路径): 列出指定 ...

  9. Spring Cloud Alibaba 使用Sentinel实现接口限流

    Sentinel是什么 Sentinel的官方标题是:分布式系统的流量防卫兵.从名字上来看,很容易就能猜到它是用来作服务稳定性保障的.对于服务稳定性保障组件,如果熟悉Spring Cloud的用户,第 ...

  10. C#Image和Icon的相互转化

    Image img = Image.FromHbitmap(icon.ToBitmap().GetHbitmap()); Graphics g = Graphics.FromImage(img); g ...