CodeForces 606C Sorting Railway Cars(最长连续上升子序列)
Description
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?
Input
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.
Output
Print a single integer — the minimum number of actions needed to sort the railway cars.
Examples
input output input output
题意:
一条无限长的铁路有一列由n个车厢组成的列车,编号从1到n(所有车辆的编号都是不同的),并按任意顺序排列。 小北极熊希望按照编号越来越大的顺序排列火车车厢。他一次可以让其中一车厢从它原本的地方消失,并将其传送到火车的起点,或者到达火车的尽头。 小北极熊为了对车厢进行排序需要执行的最少移动次数是多少?
思路:
求最长子序列,但是是数值连续的,比如1 3 5 7 2 4 6 8,最长数值连续上升序列为2,即1 2或者3 4或者5 6或者7 8,一定要数值连续,然后用总数减去这个数就是最后所求答案。
这个题用哈希的方法解决,特别巧妙。
代码:
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <stdio.h>
#define IO ios::sync_with_stdio(false);\
cin.tie();\
cout.tie();
using namespace std;
int b[];
int main()
{
IO;
int n,a;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a);
b[a]=b[a-]+;//核心代码
}
sort(b+,b+n+);//只是为了找最大值
printf("%d\n",n-b[n]);
}
附上另一个代码,实际两个代码思路相同~
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cstring>
#include <stdio.h>
#define IO ios::sync_with_stdio(false);\
cin.tie();\
cout.tie();
using namespace std;
int a[];
int main()
{
int n;
int i,j,k,l,max;
while(~scanf("%d",&n))
{
for(i = ; i <= n; i++)
{
scanf("%d",&k);
a[k] = i;
}
max = ;
for(i = ; i <= n-; i+=l)
{
l=;
for(j = i+; j <= n; j++)
{
if(a[j]>a[j-])
l++;
else
break;
}
if(l > max)
max = l;
}
printf("%d\n",n-max);
}
return ;
}
CodeForces 606C 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 335C Sorting Railway Cars
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- codeforce 606C - Sorting Railway Cars
题意:给你一串数,没个数只能往前提到首位,或则往后放末尾.问最少步骤使操作后的序列成上升序列. 思路:最长连续子序列. #include<iostream> #include<std ...
- CodeForces 605A Sorting Railway Cars
求一下最长数字连续上升的子序列长度,n-长度就是答案 O(n)可以出解,dp[i]=dp[i-1]+1,然后找到dp数组最大的值. #include<cstdio> #include< ...
- CodeForces 605A Sorting Railway Cars 思维
早起一水…… 题意看着和蓝桥杯B组的大题第二道貌似一个意思…… 不过还是有亮瞎双眼的超短代码…… 总的意思呢…… 就是最长增长子序列且增长差距为1的的…… 然后n-最大长度…… 这都怎么想的…… 希望 ...
- 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: 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 ...
随机推荐
- 开源中国愚人节网页变模糊的js blur代码
<![if !IE]> <script> /* * by moli */ $(document).ready(function(){ if(document.cookie.in ...
- 【最大流】【CODEVS】1993 草地排水
[算法]网络流-最大流(dinic) [题解]http://www.cnblogs.com/onioncyc/p/6496532.html #include<cstdio> #includ ...
- CodeForces - 1003D
Polycarp has nn coins, the value of the ii-th coin is aiai. It is guaranteed that all the values are ...
- 在Unity中实现屏幕空间反射Screen Space Reflection(3)
本篇讲一下相交检测的优化.有两个措施. 线段相交检测 之前的检测都是检测光线的终点是否在物体内.我们可以尝试检测光线的线段是否与物体相交. 比如说有一个非常薄的物体,光线差不多垂直于它的表面.如果用普 ...
- python作业Select版本FTP(第十周)
SELECT版FTP: 使用SELECT或SELECTORS模块实现并发简单版FTP 允许多用户并发上传下载文件 思路解析: 1. 使用IO多路复用的知识使用SELECTORS封装好的SELECTOR ...
- three.js_camera相机
https://blog.csdn.net/yangnianbing110/article/details/51275927 文章地址
- 转: oracle中schema指的是什么?
看来有的人还是对schema的真正含义不太理解,现在我再次整理了一下,希望对大家有所帮助. 我们先来看一下他们的定义:A schema is a collection of database obje ...
- Linux 内核进程管理之进程ID【转】
转自:http://www.cnblogs.com/hazir/p/linux_kernel_pid.html Linux 内核使用 task_struct 数据结构来关联所有与进程有关的数据和结构, ...
- UTF-8和GB2312互转的最简单快捷的方法
一.如果你想把utf-8转为GB2312 1.用记事本打开源码,把<meta http-equiv="Content-Type" content="text/htm ...
- DEDECMS如何让栏目外部链接在新窗口中打开
dede的栏目打开方式默认的“当前窗口打开”,可以用下面两种方法让dede栏目在新窗口中打开. 方法1. 查找模板中的head.htm 将 <li><a href='[field:t ...