#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <bitset>
#include <vector>
#include <stack>
typedef long long llong;
const int MAXN = 1000000 + 10;
const int MAXLOG = 20;
int n;
struct Fenwick
{
int tree[MAXN];
void update(int pos, int val)
{
for (int idx = pos ; idx < MAXN ; idx += idx & (-idx))
{
tree[idx] += val;
}
}
int query(int pos)
{
int res = 0;
for (int idx = pos ; idx > 0 ; idx -= idx & (-idx))
{
res += tree[idx];
}
return res;
}
int findKth(int k)
{
int pos = 0;
for (int log = MAXLOG - 1 ; log >= 0 ; --log)
{
if (pos + (1 << log) < MAXN && tree[pos + (1 << log)] < k)
{
pos += (1 << log);
k -= tree[pos];
}
}
return pos + 1;
}
};
int a[MAXN];
int dp[MAXN];
int prefix[MAXN];
bool endHere[MAXN];
std::stack <int> st;
Fenwick tree;
void solve()
{
for (int i = 1 ; i <= n ; ++i)
{
prefix[i] = prefix[i - 1] + (a[i - 1] > a[i]);
tree.update(a[i], 1);
}
for (int i = n ; i >= 1 ; --i)
{
tree.update(a[i], -1);
int allowedTo = tree.findKth(tree.query(a[i]) + 1);
int l = i, r = n + 1, mid;
while (l < r - 1)
{
mid = (l + r) / 2;
if (a[mid] <= allowedTo && prefix[mid] - prefix[i] == 0)
{
l = mid;
} else
{
r = mid;
}
}
int min = n;
for (int idx = i ; idx <= l ; ++idx)
{
min = std::min(min, dp[idx + 1]);
}
dp[i] = min + 1;
}
std::cout << dp[1] << '\n';
}
void input()
{
std::cin >> n;
for (int i = 1 ; i <= n ; ++i)
{
std::cin >> a[i];
}
}
void fastIOI()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int main()
{
fastIOI();
input();
solve();
return 0;
}
/*
8
7 6 8 1 5 2 4 3
7 | 5 7 | 1 4 | 1 2 | 1
1 | 1 |1 2| 4 | 5 7 | 7
7 5 7 1 4 1 2 1
5
7 | 5 7 | 1 5 2 4 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6556 KB |
Output is correct |
11 |
Correct |
1 ms |
6612 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6612 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6556 KB |
Output is correct |
11 |
Correct |
1 ms |
6612 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6612 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6488 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
1 ms |
6496 KB |
Output is correct |
22 |
Correct |
2 ms |
6744 KB |
Output is correct |
23 |
Correct |
1 ms |
4448 KB |
Output is correct |
24 |
Correct |
1 ms |
6496 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6556 KB |
Output is correct |
11 |
Correct |
1 ms |
6612 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6612 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6488 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
1 ms |
6496 KB |
Output is correct |
22 |
Correct |
2 ms |
6744 KB |
Output is correct |
23 |
Correct |
1 ms |
4448 KB |
Output is correct |
24 |
Correct |
1 ms |
6496 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6500 KB |
Output is correct |
27 |
Correct |
1 ms |
6512 KB |
Output is correct |
28 |
Correct |
1 ms |
6620 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
6488 KB |
Output is correct |
35 |
Correct |
1 ms |
6488 KB |
Output is correct |
36 |
Correct |
2 ms |
6488 KB |
Output is correct |
37 |
Correct |
1 ms |
6748 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6556 KB |
Output is correct |
11 |
Correct |
1 ms |
6612 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6612 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6488 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6748 KB |
Output is correct |
21 |
Correct |
1 ms |
6496 KB |
Output is correct |
22 |
Correct |
2 ms |
6744 KB |
Output is correct |
23 |
Correct |
1 ms |
4448 KB |
Output is correct |
24 |
Correct |
1 ms |
6496 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6500 KB |
Output is correct |
27 |
Correct |
1 ms |
6512 KB |
Output is correct |
28 |
Correct |
1 ms |
6620 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
1 ms |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6492 KB |
Output is correct |
34 |
Correct |
1 ms |
6488 KB |
Output is correct |
35 |
Correct |
1 ms |
6488 KB |
Output is correct |
36 |
Correct |
2 ms |
6488 KB |
Output is correct |
37 |
Correct |
1 ms |
6748 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Correct |
125 ms |
17596 KB |
Output is correct |
40 |
Correct |
226 ms |
20784 KB |
Output is correct |
41 |
Correct |
98 ms |
14160 KB |
Output is correct |
42 |
Correct |
93 ms |
11600 KB |
Output is correct |
43 |
Correct |
76 ms |
10064 KB |
Output is correct |
44 |
Correct |
280 ms |
23272 KB |
Output is correct |
45 |
Correct |
259 ms |
22960 KB |
Output is correct |
46 |
Correct |
252 ms |
23108 KB |
Output is correct |
47 |
Correct |
231 ms |
23120 KB |
Output is correct |
48 |
Correct |
220 ms |
23124 KB |
Output is correct |
49 |
Correct |
339 ms |
23376 KB |
Output is correct |
50 |
Correct |
321 ms |
23248 KB |
Output is correct |
51 |
Correct |
317 ms |
23348 KB |
Output is correct |
52 |
Correct |
364 ms |
23124 KB |
Output is correct |
53 |
Correct |
311 ms |
23364 KB |
Output is correct |
54 |
Correct |
331 ms |
22864 KB |
Output is correct |
55 |
Correct |
392 ms |
23380 KB |
Output is correct |
56 |
Correct |
390 ms |
23124 KB |
Output is correct |
57 |
Correct |
390 ms |
23380 KB |
Output is correct |
58 |
Correct |
368 ms |
22972 KB |
Output is correct |
59 |
Correct |
405 ms |
23376 KB |
Output is correct |
60 |
Correct |
402 ms |
23188 KB |
Output is correct |
61 |
Correct |
391 ms |
23248 KB |
Output is correct |
62 |
Correct |
443 ms |
23188 KB |
Output is correct |