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 ...
随机推荐
- react学习总结
http://www.runoob.com/react/react-tutorial.html一般先看一些中文的简单的介绍和一些基本概念http://reactjs.cn/react/docs/get ...
- KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架之koahub-handlebars
koahub-handlebars koahub-handlebars koahub handlebars templates Installation $ npm install koahub-ha ...
- 485. Max Consecutive Ones
题目 Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: ...
- HDFS Namenode启动过程
文章作者:luxianghao 文章来源:http://www.cnblogs.com/luxianghao/p/6564032.html 转载请注明,谢谢合作. 免责声明:文章内容仅代表个人观点, ...
- Win10上编译CoreCLR的Windows和Linux版本
一.编译环境 首先,不管是Windows还是Linux版本CoreCLR的编译,都是在Windows10上进行的. 二.CoreCLR for Windows 在Windows上做编译怎么能少得了Vi ...
- ICC_lab总结——ICC_lab4:时钟树综合
时钟树综合的理论知识总结在这里:http://www.cnblogs.com/IClearner/p/6580034.html 下面是实践环节:使用ICC进行时钟树综合. 这个实验的目标是: ·设置C ...
- 写入soap消息以及与soap消息通信
1.些了解一下soap消息的结构以及通信方式如下图:
- 学习CSS了解单位em和px的区别
这里引用的是Jorux的“95%的中国网站需要重写CSS”的文章,题目有点吓人,但是确实是现在国内网页制作方面的一些缺陷.我一直也搞不清楚px与em之间的关系和特点,看过以后确实收获很大.平时都是用p ...
- ajax ----进度条的原理
一.进度条的原理 新知识点:Html5中FormData,xmlHttpRequest中的upload属性,progress事件监控 xmlHttpRequest中的upload属性,实现: < ...
- Nginx网站使用CDN之后禁止用户真实IP访问的方法
做过面向公网WEB的运维人员经常会遇见恶意扫描.拉取.注入等图谋不轨的行为,对于直接对外的WEB服务器,我们可以直接通过 iptables .Nginx 的deny指令或是程序来ban掉这些恶意请求. ...