CF #335 div1 A. Sorting Railway Cars
题目链接:http://codeforces.com/contest/605/problem/A
大意是对一个排列进行排序,每一次操作可以将一个数字从原来位置抽出放到开头或结尾,问最少需要操作多少次可以将原排列变为有序。
一个比较很想当然的算法是用长度减去最长上升子序列,但这是错误的。
反例:
5
1 3 4 5 2
按照n-lis,会得出答案1,但显然这是做不到的,答案应是2
正解应当是考虑最终变为有序时,所有未经操作的数字,应当是连续的。所以要使得操作次数最少就要求在原来数列中位置递增的最长连续数段。
以上面为例,则3 4 5这3个连续数字组成的数段满足在原来数列中位置递增,且是最长的数段。
代码如下:
readInts=lambda: list(map(int, input().split()))
n=int(input())
a=readInts()
p=[0]*n
for i in range(n):
p[a[i]-1]=i
inc=1;ret=n-1
for i in range(1,n):
if p[i]>p[i-1]:
inc+=1
else:
inc=1
ret=min(ret,n-inc)
print(ret)
CF #335 div1 A. Sorting Railway Cars的更多相关文章
- 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 ...
- 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 ...
- 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 335C Sorting Railway Cars
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- 【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/E Description An infinitely lon ...
- [Codeforces 606C]Sorting Railway Cars
Description An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the n ...
随机推荐
- [HDU 2102] A计划(搜索题,典型dfs or bfs)
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 选择 GCD 还是 NSTimer
我们常常会延迟某件任务的执行,或者让某件任务周期性的执行.然后也会在某些时候需要取消掉之前延迟执行的任务. 延迟操作的方案一般有三种: 1.NSObject的方法: gcdTimer 2.使用NSTi ...
- 3892: [Usaco2014 Dec]Marathon
3892: [Usaco2014 Dec]Marathon Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 169 Solved: 100[Submi ...
- iOS10构建版本不显示的问题
iOS10,构建版本问题: 在Xcode中->product->archive,进行相关操作后,upload后没有报错验证也成功的情况下,在开发者账号构建版本号里面迟迟没有显示的原因: i ...
- H5 Canvas vs. SVG
HTML 5 Canvas vs. SVG HTML5 SVG HTML5 地理定位 Canvas 和 SVG 都允许您在浏览器中创建图形,但是它们在根本上是不同的. SVG SVG 是一种使用 XM ...
- python 接口自动化测试--框架定型(六)
脚本执行步骤: 1.还原测试数据库: 2.读取接口用例CSV文件到数据库: 3.执行数据库中标记执行的用例: 4.对比预期结果,将测试结果写入数据库结果表中. 数据管理: 事先备份测试数据库,并搭建自 ...
- ps-抠图
1- 图层区—复制背景图层 防止原图修改失败后无法还原 2- 工具栏——磁性套索工具 可以有效的对色彩边线较为明显的图片进行抠图 ...
- JS 拖动DIV 需要JQUERY 支持
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 时间相关库<ctime>解析
原创作品,转载请注明来源:http://www.cnblogs.com/shrimp-can/p/5649487.html 一.定义的类型 1.clock_t:时钟类型 2.size_t:unsign ...
- 运行错误:应用程序无法启动因为并行配置不正确。the application has failed to start because its side-by-side configuration is incorrect 解决方法
问题描述: 当电脑同时安装VS2008和VS2008 SP1时,编译出来的Visual C++程序的manifest 文件会默认引用VS2008的MFC版本和CRT版本.如下: <depende ...