Codeforces Round #335 (Div. 2) 606C Sorting Railway Cars(hash)
来源:互联网 发布:拜尔电动牙刷知乎 编辑:程序博客网 时间:2024/06/07 18:55
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.
54 1 2 5 3
2
44 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.
题目链接:点击打开链接
给出一个序列, 任一个数可以放到序列末尾或者最前, 输出最小操作数.
hash一下每个数出现的位置, n减最长连续上升长度即为答案.
AC代码:
#include "iostream"#include "cstdio"#include "cstring"#include "algorithm"#include "queue"#include "stack"#include "cmath"#include "utility"#include "map"#include "set"#include "vector"#include "list"#include "string"using namespace std;typedef long long ll;const int MOD = 1e9 + 7;const int INF = 0x3f3f3f3f;const int MAXN = 1e5 + 5;int n, len = 1, ans, a[MAXN], b[MAXN];int main(int argc, char const *argv[]){ scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%d", &a[i]); b[a[i]] = i; } for(int i = 2; i <= n; ++i) { if(b[i] > b[i - 1]) len++; else { ans = max(len, ans); len = 1; } } ans = max(ans, len); printf("%d\n", n - ans); return 0;}
- Codeforces Round #335 (Div. 2) 606C Sorting Railway Cars(hash)
- Codeforces Round #335 (Div. 2) C.Sorting Railway Cars
- Codeforces Round #335 (Div. 2) C. Sorting Railway Cars
- Codeforces Round #335 (Div. 2)C. Sorting Railway Cars
- Codeforces Round #335 (Div. 2) 605A Sorting Railway Cars
- Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 最长连续上升子序列
- Codeforces Round #335 (Div. 2)-C Sorting Railway Cars(最长上升子序列)
- Codeforces Round #335 (Div. 1) A. Sorting Railway Cars
- Codeforces Round #335 (Div. 2)-Sorting Railway Cars(求连续的上升序列的最大值)
- Codeforces 606 C Sorting Railway Cars【思维】
- Codeforces 606 C Sorting Railway Cars
- codeforces 335 C Sorting Railway Cars
- 【Codeforces Round 335 (Div 2) C】【贪心 脑洞 有趣排序】Sorting Railway Cars 全排列排序可以提前后提后的最小操作数
- CodeForces 606C--Sorting Railway Cars,思路题~~~
- codeforces C. Sorting Railway Cars 贪心
- CF 335 div.2-C/div.1-A/605A Sorting Railway Cars
- C - Sorting Railway Cars
- C. Sorting Railway Cars
- iOS开发 打印CGRect时用NSStringFromCGRect
- 【Codeforces Round 273 (Div 2)C】【贪心 脑洞】 Table Decorations 三种气球装饰桌子 同桌三个气球不完全同色的最多桌子装饰数
- 【LEETCODE】241-Different Ways to Add Parentheses
- 转载一篇android裁剪图片的解决方案,个人呢觉得很好的
- 基于暗通道去雾算法的实现与优化(二)opencv在pc上的实现
- Codeforces Round #335 (Div. 2) 606C Sorting Railway Cars(hash)
- duilib获取CSliderUI滑动事件 - 用自定义控件实现
- IOS-42-谓词的使用(例:检查是否为手机号)
- 151211Exception : Could not load NIB in bundle
- C++学习笔记(初级篇)——作用域与“extern”关键字
- 简单易作的晶振测试
- 在云平台上基于Go语言+Google图表API提供二维码生成应用
- Git Bash下载Android源码
- 将自己的驱动加到内核