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 ...
随机推荐
- DIV+CSS制作斜线效果记录
DIV+CSS 斜线效果很简单,只需设置一下CSS Border 的边框就能有斜线效果.代码分享给大家,你可以自己变通. 提示要注意两点:1.DIV宽高的定义.2.DIV在 IE6 中默认是有高度的. ...
- HDU 2082 找单词 (普通母函数)
题目链接 Problem Description 假设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于 ...
- 从python入门ruby
1.Ruby的函数可以不使用括号 def h(name) puts "hello #{name}" end h "jack" 2.python可以直接访问实例的 ...
- 深入理解C指针----学习笔记
深入理解C指针 第1章 认识指针 理解指针的关键在于理解C程序如何管理内存,指针包含的就是内存地址. 1.1 指针和内存 C程序在编译后,以三种方式使用内存: 1. 静态. ...
- JS的全局函数eval解析JSON字符串
JavaScript eval() 函数 定义和用法 eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 语法 eval(string) 参数 描述 string 必需. ...
- javascript反混淆之packed混淆(二)
上次我们简单的入门下怎么使用html破解packed的混淆,下面看一个综合案例. 上次内容javascript反混淆之packed混淆(一) function getKey() { var aaaaf ...
- C/C++——库函数strcpy和strdup比较
版权声明:原创文章,禁止转载. 1. strcpy 原型: extern char *strcpy(char *dest,char *src); 用法: #include <string.h&g ...
- Maven如何发布项目到一个Tomcat中
首先,在本地tomcat的conf/tomcat-users.xml 中配置一个user,准备让maven接入时使用: <role rolename="admin-gui"/ ...
- untiy3d学习笔记
Unity3d 记录 1.63讲 主要讲了menicam 从3D软件里面导出过后,注意如果是人物模型命名一定要非常清晰并且对称.选择到模型后等到到humanoid后可以使用menicam.然后使用me ...
- ASP.NET Core 2.0 MVC 发布部署--------- linux安装.NET CORE SDK具体操作链接以及操作总细节
具体链接:https://www.microsoft.com/net/learn/get-started/linuxubuntu 如下图: