Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 连续LIS
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.
题意:给n长度的序列,每次能把任意位置的数移到序列头,序列尾算一个操作,问你最少经过几次操作使其有序;
题解:也就是最长连续子序列
//meek
///#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************** const int N=+;
const ll inf = 1ll<<;
const int mod= ; int ans,a[N],H[N],n;
int dp[N];
int main() {
ans=-;
scanf("%d",&n);
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
}
for(int i=;i<=n;i++) {
H[a[i]] ++;
if(H[a[i]-]) dp[a[i]]=dp[a[i]-]+;
else dp[a[i]]=;
ans=max(ans,dp[a[i]]);
}
cout<<n-ans<<endl;
return ;
代码
Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 连续LIS的更多相关文章
- 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 ...
- 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 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) 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/ ...
随机推荐
- IOS中 如何去除Tabview里面cell之间的下划线
可以利用Tabview的separatorStyle属性来设置,选择其中的UITableViewCellSeparatorStyleNone 即可去除cell之间的下划线 self.tableView ...
- windows phone 8.1开发笔记之Toast
Toast(吐司)是wp屏幕上端弹出来的临时通知,他会存在7秒钟的时间,可以快速定位到用户需要的位置(当然是由开发者设置的) 1.创建一个Toast now,需要将你的app设置支持Toast 否则即 ...
- libmemcached upcoming ISO C++ standard, C++0x
在编译我的小程序的时候,触发了一个编译错误,程序中使用了libmemcached,错误如下: 1 2 3 4 5 6 7 8 9 In file included from /usr/lib/gcc/ ...
- 不靠谱的Paypal及海外网站
昨天因为报名参加某个比赛,需要用到Paypal付款,整整折腾了我一个小时.我先是使用自己的银行储蓄卡,到最后一步需要银行确认,提示我需要安装个插件才能输入密码.安装完插件后需要重启浏览器,整个流程又要 ...
- ios 中使用SBJson拼接和解析json
1.ios解析json 使用开源json包,项目地址: http://stig.github.com/json-framework/ NSData * responseData = [res ...
- Swift Tips - 在 Swift 中自定义下标访问
Untitled Document.md input[type="date"].form-control,.input-group-sm>input[type="d ...
- iOS8 超简单的设置圆角按钮 ImageView等UIView
button.layer.cornerRadius = // 这个值根据你想要的效果可以更改 button.clipsToBounds = true 这种方法不止可以设置按钮,UIView应该都可以设 ...
- python 安装scrapy
1. 首先你先得安装PYTHON...还是推荐2.7吧,之前装了3.3似乎和这个世界格格不入...先装个2.7. 并将python加入系统的环境变量. 2. 去scrapy 官网下载最新版本的scra ...
- 解决SQLite数据库中文乱码问题
关于SQLite中出现中文乱码的分析以及解决方案 我们在使用SQLite数据库时候,可能会发现,向数据库插入数据时候显示的是汉字,但通过SQLite读出来时却显示的乱码,这是因为SQLite数据库 ...
- JAVA类与对象(五)----对象的生成、使用
对象的生成 创建一个对象包括对象的声明.实例化.初始化三部分. 1.声明-----类名对象名 声明并不是为对象分配内存空间,而只是分配一个引用空间.对象的引用类似于指针,是32位的地址空间,它的值指向 ...