Codeforces 335C Sorting Railway Cars
2 seconds
256 megabytes
standard input
standard output
An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?
The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train.
The second line contains n integers pi (1 ≤ pi ≤ n, pi ≠ pj if i ≠ j) — the sequence of the numbers of the cars in the train.
Print a single integer — the minimum number of actions needed to sort the railway cars.
5
4 1 2 5 3
2
4
4 1 3 2
2
In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train.
题意:给你一个1~n的一个排列,一次操作是指把选一个数移动到头或尾,求使序列递增的最小操作数
思路:我们需要找一个最长的递增子序列b1,b2,b3..bm,且该子序列要满足bm = bm-1 + 1. 比如4 1 2 5 3 满足的最长递增子序列是1 2 3,那么答案最终是n -m
即我们找到这样的一个子序列后,对于其他的数,只要从小到达移到到该子序列的两端即可
r[i]表示第i大的元素,d[i]表示比i小1的数在i的左边还是右边
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long ll;
const int N = ;
int dp[N], a[N], d[N], r[N];
int cmp(int b, int c) {
return a[b] < a[c];
}
int main()
{
int n;
scanf("%d", &n);
for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
for(int i = ; i <= n; ++i) r[i] = i;
sort(r + , r + n + , cmp);
d[] = ;
for(int i = ; i < n; ++i)
{
if(r[i] < r[i + ]) d[i + ] = ;
else d[i + ] = ;
}
dp[ r[] ] = ;
int ans = ;
for(int i = ; i <= n; ++i)
{
if(d[i]) dp[ r[i] ] = dp[ r[i-] ] + ;
else dp[ r[i] ] = ;
ans = max(ans, dp[ r[i] ]);
}
printf("%d\n", n - ans);
return ;
}
Codeforces 335C Sorting Railway Cars的更多相关文章
- [Codeforces 606C]Sorting Railway Cars
Description An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the n ...
- CodeForces 606C Sorting Railway Cars(最长连续上升子序列)
Description An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the n ...
- CodeForces 605A Sorting Railway Cars 思维
早起一水…… 题意看着和蓝桥杯B组的大题第二道貌似一个意思…… 不过还是有亮瞎双眼的超短代码…… 总的意思呢…… 就是最长增长子序列且增长差距为1的的…… 然后n-最大长度…… 这都怎么想的…… 希望 ...
- CodeForces 605A Sorting Railway Cars
求一下最长数字连续上升的子序列长度,n-长度就是答案 O(n)可以出解,dp[i]=dp[i-1]+1,然后找到dp数组最大的值. #include<cstdio> #include< ...
- 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 连续LIS
C. Sorting Railway Cars An infinitely long railway has a train consisting of n cars, numbered from ...
- 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 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 ...
- CF#335 Sorting Railway Cars
Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- library not found for -lAFNetworking
错误内容如图所示: error:linker command failed with exit code 1(use -v to see invocation) 首先报这个错的情况有很多,所以需要看e ...
- ASP.net绑定文本框Enter事件到按钮 ASP.NET执行后台执行JS方法
txtAccountBarcode.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if (( ...
- Javascript异步编程方法总结
现在我们有三个函数,f1, f2, f3 按正常的思路我们会这样写代码: function f1 (){}; function f2 (){}; function f3 (){}; //在这里调用函数 ...
- gcc提供的原子操作函数
gcc从4.1.2提供了__sync_*系列的built-in函数,用于提供加减和逻辑运算的原子操作.其声明如下: type __sync_fetch_and_add (type *ptr, type ...
- UINavigationController导航条是否挡住下面的内容
控制 UINavigationController 导航条是否挡住下面的内容 if ([[[UIDevice currentDevice] systemVersion] floatValue] > ...
- 四、优化及调试--网站优化--Yahoo军规上
什么是Yahoo军规?即如何提高网站速度的知识. 具体如下: 1.尽量减少HTTP请求个数——须权衡 什么是http请求:从客户端到服务器端的请求消息.包括消息首行中,对资源的请求方法,资源的标识符及 ...
- C#学习笔记----栈与堆的知识
http://my.oschina.net/lichaoqiang/blog/291906 当我们对.NET Framework的一些基本面了解之后,实际上,还是很有必要了解一些更底层的知识.比如.N ...
- 谈谈Delphi中的类和对象1---介绍几个概念 && 对象是一个地地道道的指针
参考:http://blog.163.com/liang_liu99/blog/static/88415216200952123412180/ 以下的介绍主要针对的是Delphi的面向对象的知识,可能 ...
- poj 1008:Maya Calendar(模拟题,玛雅日历转换)
Maya Calendar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64795 Accepted: 19978 D ...
- C编译: 动态连接库 (.so文件)(转摘)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在“纸上谈兵: 算法与数据结构”中,我在每一篇都会有一个C程序,用于实现算法和数据 ...