传送门

题目大意

给出一个 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. js 调用接口并传参

    注:需先引入 jquery.json-xx.min.js 1. 参数跟在url后面 var name = '王一'; var age = 18; $.ajax({ type : 'get', url ...

  2. java笔试之参数解析(正则匹配)

    在命令行输入如下命令: xcopy /s c:\ d:\, 各个参数如下: 参数1:命令字xcopy 参数2:字符串/s 参数3:字符串c:\ 参数4: 字符串d:\ 请编写一个参数解析程序,实现将命 ...

  3. Mysql图解安装向导

    注:本次安装为解压缩版: 1.设置Mysql环境变量: MYSQL_HOME: D:\Java\MySql\mysql-5.7.9-winx64 PATH: %MYSQL_HOME%\bin; 2.安 ...

  4. spring自定义bean工厂模式解耦

    在resources下创建bean.properties accountService=cn.flypig666.service.impl.AccountServiceImpl accountDao= ...

  5. ArrayList去除重复元素(多种方法实现)

    package other; import java.util.ArrayList; import java.util.HashSet; public class test4 { public sta ...

  6. php curl的隐藏BUG

    <?php $a = array( 'a' => 2, 'b' => 3, ); $curl = curl_init(); $b = $a; curl_setopt_array($c ...

  7. TmodJs:常用语法

    ylbtech-TmodJs:常用语法 1.返回顶部 1.循环 {{each items as item index}} <tr> <td>{{index+1}}</td ...

  8. Origin使用自定义函数拟合曲线函数

    (2019年2月19日注:这篇文章原先发在自己github那边的博客,时间是2016年10月28日) 最近应该是六叔的物化理论作业要交了吧,很多人问我六叔的作业里面有两道题要怎么进行图像函数的拟合.综 ...

  9. springmvc 文件下载分批读取,防止内存溢出

    参考 https://blog.csdn.net/u014732956/article/details/51404086    

  10. mtk 的conferrence call建立流程

    (重点看main_log与) 抓mtk log: 1.*#*#82533284#*#*      进入抓log UI 2.*#*#825364#*#*      进入工程模式 3.进入"Lo ...