Codeforces Round #335 (Div. 2) C. 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,2,3,4,。。。。n;
所以我本来想求最长公共子序列,发现时间复杂度太大;
后来想因为一定上升只要求最长连续上升即可;
#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define esp 1e-13
const int N=1e5+,M=1e6+,inf=1e9+,mod=;
int a[N];
int dp[N];
int flag[N];
int main()
{
int x,y,z,i,t;
scanf("%d",&x);
for(i=;i<=x;i++)
scanf("%d",&y),flag[y]=i;
dp[]=;
for(i=;i<=x;i++)
{
dp[i]=;
if(flag[i]>flag[i-])
dp[i]=dp[i-]+;
}
int ans=;
for(i=;i<=x;i++)
ans=max(ans,dp[i]);
printf("%d\n",x-ans);
return ;
}
Codeforces Round #335 (Div. 2) C. Sorting Railway Cars的更多相关文章
- 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 Round #335 (Div. 2)
水 A - Magic Spheres 这题也卡了很久很久,关键是“至少”,所以只要判断多出来的是否比需要的多就行了. #include <bits/stdc++.h> using nam ...
- Codeforces Round #335 (Div. 2) C
C. Sorting Railway Cars time limit pe ...
- Codeforces Round #335 (Div. 2) B. Testing Robots 水题
B. Testing Robots Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contest/606 ...
- Codeforces Round #335 (Div. 1) C. Freelancer's Dreams 计算几何
C. Freelancer's Dreams Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contes ...
- Codeforces Round #335 (Div. 2) D. Lazy Student 构造
D. Lazy Student Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/606/probl ...
- Codeforces Round #335 (Div. 2) A. Magic Spheres 水题
A. Magic Spheres Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.codeforces.com/contest/606/ ...
- Codeforces Round #335 (Div. 2) D. Lazy Student 贪心+构造
题目链接: http://codeforces.com/contest/606/problem/D D. Lazy Student time limit per test2 secondsmemory ...
随机推荐
- python easy install时,使用aliyun阿里云镜像提示主机名不匹配的问题
因网络问题,因此设置 easy_install 使用阿里云的源, ## 更新 easy_install 源 tee ~/.pydistutils.cfg <<-'EOF' [easy_in ...
- bootstrap-ui-datetime-picker插件学习
GitHub:https://github.com/Gillardo/bootstrap-ui-datetime-picker 准备 安装:bower install --save bootstrap ...
- Ubuntu配置apache2.4配置虚拟主机遇到的问题
update: 偶然看到了 apache的更新说明,直接贴个地址过来吧. http://httpd.apache.org/docs/2.4/upgrading.html 最近想把web开发目录从/va ...
- ObjC消息机制
深入浅出ObjC之消息 罗朝辉(http://blog.csdn.net/kesalin) 在入门级别的ObjC 教程中,我们常对从C++或Java 或其他面向对象语言转过来的程序员说,ObjC ...
- 由浅到深理解ROS(3)-命名空间
全局命名空间: /rosout前面的反斜杠“/”表明该节点名称属于全局命名空间.之所以叫做全局名称因为它们在任何地方(包括代码.命令行工具.图形界面工具等的任何地方)都可以使用.无论这些名称用作众多命 ...
- @media screen 针对不同移动设备——响应式设计
概念: device-pixel-ratio:定义输入设备屏幕的可视宽度与可见高度比率. device-width:输入设备屏幕的可视宽度. orientation :屏幕横竖屏定向.landscap ...
- php配置opcache
官网地址:http://php.net/opcache 使用下列推荐设置来获得较好的 性能: opcache.memory_consumption=128 opcache.interned_strin ...
- jsp安全性问题
jsp项目不同jsp之间假设只通过超链接进行跳转,安全性太低,不能满足现实生活中对安全性的要求! 为了提高安全性.能够通过Servlet进行跳转,进行跳转的时候为了进一步实现其安全性,能够通过间jsp ...
- Jmeter监控Linux服务器性能
①.下载JMeterPlugins相关的jar包,放jmeter的安装路径\lib\ext下——这个时候启动jmeter会发现,添加监听器时,出现了一堆的jp@jc……,这些就是插件的功劳. JMet ...
- Android设备各种使用尺寸整理
// 获取屏幕的宽度.高度 Display defDip = getWindowManager().getDefaultDisplay(); int disWidth = defDip.getWidt ...