CF#335 Sorting Railway Cars

Sorting Railway Cars
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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?

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 ≤ npi ≠ 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.

Sample test(s)
input
5
4 1 2 5 3
output
2
input
4
4 1 3 2
output
2
Note

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-n的n个数,各不相同,每一步可以将任意一个取出,放在开头或者结尾,问把这个序列变回1-n,要多少步。

分析:显然,每个数最多取出一次,否则没有意义。

这样的话,我们可以将取出和放下(放在开头或者结尾)分开考虑。

取出之后剩下的东西一定是一个连续的上升子序列,当这个序列最长时,答案最优。

因为可以调整取出顺序,放回当然按大小顺序放回(即按大小顺序取出)就好。

注意一定是连续的上升子序列,这里的连续指的是数值上的连续。

 1 /**
 2 Create By yzx - stupidboy
 3 */
 4 #include <cstdio>
 5 #include <cstring>
 6 #include <cstdlib>
 7 #include <cmath>
 8 #include <deque>
 9 #include <vector>
10 #include <queue>
11 #include <iostream>
12 #include <algorithm>
13 #include <map>
14 #include <set>
15 #include <ctime>
16 #include <iomanip>
17 using namespace std;
18 typedef long long LL;
19 typedef double DB;
20 #define MIT (2147483647)
21 #define INF (1000000001)
22 #define MLL (1000000000000000001LL)
23 #define sz(x) ((int) (x).size())
24 #define clr(x, y) memset(x, y, sizeof(x))
25 #define puf push_front
26 #define pub push_back
27 #define pof pop_front
28 #define pob pop_back
29 #define mk make_pair
30 
31 inline int Getint()
32 {
33     int Ret = 0;
34     char Ch = ' ';
35     bool Flag = 0;
36     while(!(Ch >= '0' && Ch <= '9'))
37     {
38         if(Ch == '-') Flag ^= 1;
39         Ch = getchar();
40     }
41     while(Ch >= '0' && Ch <= '9')
42     {
43         Ret = Ret * 10 + Ch - '0';
44         Ch = getchar();
45     }
46     return Flag ? -Ret : Ret;
47 }
48 
49 const int N = 100010;
50 int n, arr[N];
51 int cnt[N];
52 
53 inline void Input()
54 {
55     scanf("%d", &n);
56     for(int i = 1; i <= n; i++) scanf("%d", &arr[i]);
57 }
58 
59 inline void Solve()
60 {
61     for(int i = 1; i <= n; i++)
62         cnt[arr[i]] = cnt[arr[i] - 1] + 1;
63     int ans = 0;
64     for(int i = 1; i <= n; i++)
65         ans = max(ans, cnt[i]);
66     printf("%d\n", n - ans);
67 }
68 
69 int main()
70 {
71     freopen("a.in", "r", stdin);
72     Input();
73     Solve();
74     return 0;
75 }
View Code

 

posted @ 2015-12-21 09:25  yanzx6  阅读(271)  评论(0编辑  收藏  举报