-
Notifications
You must be signed in to change notification settings - Fork 1
/
sjtu-oj1062.cpp
78 lines (66 loc) · 1.5 KB
/
sjtu-oj1062.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <memory.h>
using namespace std;
int search(int *dp, int tar, int l, int r)
{
if (r == l) return l;
int mid = (l + r) / 2;
if (dp[mid] >= tar) return search(dp, tar, l, mid);
else return search(dp, tar, mid + 1, r);
}
void fillin(int *h, int *leng, int *dp, int n)
{
int cur_len = 0;
for (int i = 0; i < n; i++)
{
int pos = search(dp, h[i], 0, cur_len);
dp[pos] = h[i];
if (pos == cur_len)
cur_len++;
leng[i] = cur_len;
}
}
int main()
{
int n;
scanf("%d", &n);
int *h = new int[n];
int *rev_h = new int[n];
for (int i = 0; i < n; i++)
{
scanf("%d", &h[i]);
rev_h[n - 1 - i] = h[i];
}
int *dp1 = new int[n];
int *dp2 = new int[n];
int *for_len = new int[n];
int *bac_len = new int[n];
memset(dp1, 0, sizeof(int) * n);
memset(for_len, 0, sizeof(int) * n);
fillin(h, for_len, dp1, n);
memset(dp2, 0, sizeof(int) * n);
memset(bac_len, 0, sizeof(int) * n);
fillin(rev_h, bac_len, dp2, n);
int ans = 0;
int tmp;
for (int i = 0; i < n; i++)
{
tmp = for_len[i] + bac_len[n - 1 - i] - 1;
if (tmp > ans) ans = tmp;
}
ans = n - ans;
printf("%d\n", ans);
//for (int i = 0; i < n; i++)
// printf("%d ", dp1[i]);
//printf("\n");
//for (int i = 0; i < n; i++)
// printf("%d ", dp2[i]);
//printf("\n");
//for (int i = 0; i < n; i++)
// printf("%d ", for_len[i]);
//printf("\n");
//for (int i = 0; i < n; i++)
// printf("%d ", bac_len[i]);
//printf("\n");
return 0;
}