题目链接: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的更多相关文章

  1. CF#335 Sorting Railway Cars

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

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

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

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

  5. A. Sorting Railway Cars

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

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

  7. Codeforces 335C Sorting Railway Cars

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

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

  9. [Codeforces 606C]Sorting Railway Cars

    Description An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the n ...

随机推荐

  1. 从零开始学习Vue(三)

    我们从一个例子来学习组件,vuejs2.0实战:仿豆瓣app项目,创建自定义组件tabbar 这个例子用到其他组件,对于初学者来说,一下子要了解那么多组件的使用,会变得一头雾水.所以我把这个例子改写了 ...

  2. ehcache memcache redis 区别

    之前用过redis 和 memcache ,没有ehcache 的开发经验,最近也查阅不少文档和博客,写一些总结,也有不少内容总结与诸多博客中的博主总结:  Ehcache EhCache 是一个纯J ...

  3. IO 模型

    常见的IO模型有四种: (1)同步阻塞IO(Blocking IO):即传统的IO模型. (2)同步非阻塞IO(Non-blocking IO):默认创建的socket都是阻塞的,非阻塞IO要求soc ...

  4. android 仿摩拜单车共享单车进度条实现StepView

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6552712.html 先看效果图: Step1:定义StepBean 定义五个状态,分别为:为完成.正在进行 ...

  5. ASP.NET脚本过滤-防止跨站脚本攻击(收集别人的)

    ASP.Net 1.1后引入了对提交表单自动检查是否存在XSS(跨站脚本攻击)的能力.当用户试图用<xxxx>之类的输入影响页面返回结果的时候,ASP.Net的引擎会引发一个 HttpRe ...

  6. centos7 docker安装详解

    查看内核和操作系统版本[root@prod3 ~]# uname -r3.10.0-327.el7.x86_64[root@prod3 ~]# cat /etc/redhat-release Cent ...

  7. Docker 使用指南 (四)—— 数据卷的使用

    一.数据卷的使用 有时候需要使用数据库,但是又希望它的数据能保存在本地,Docker中提供了数据卷可以供你方便的操作数据.数据卷是一个可供一个或多个容器使用的特殊目录,它绕过 UFS,可以提供很多有用 ...

  8. shp文件的读取

    http://blog.csdn.net/q_l_s/article/details/41486813

  9. 安装psacct或acct程序包

    监视Linux用户活动 我认为,对每个想密切监视其服务器/系统上用户活动的Linux/Unix系统管理员来说,psacct或acct是优秀的.必需的应用程序之一. psacct或acct程序包提供了用 ...

  10. 如何给sublime text3安装汉化包?so easy 哦

    这是我本人亲身测试过的,肯定有效,没用的话怪我咯. 首先安装package control,然后安装汉化包即可,很简单哦!!! 1.安装package control:打开sublime,使用快捷键: ...