/**
* It took me long before I can even find 1 dp solution. At some point, I started to
* consider the heights in decreasing order. Let the height of the highest
* mountain(s) be h, and let m_1, m_2, ..., m_k be the mountain(s) attaining h from
* left to right. Clearly, {m_1, ..., m_k} can see each other, so at most one of them
* can be chosen. Now, the remaining mountains are partitioned into (k+1) contigous
* segments, and we know that mountains from different segments can't see each other
* (cuz they're separated by a mountain with height h)! In other words, the segments
* are "somewhat independent", prompting us to consider range dp.
*
* Let's do some rigorous reasoning. Suppose that we're finding the max num of deevs
* that can be imprisoned with mountains [i,j]. If mountain i is not chosen, the ans
* is the same as [i+1,j]'s. If mountain i (s) is chosen, we notice sth~
* Let m_1, m_2, ..., m_l be the mountains (in [i+1,j] from left to right) that can
* be seen by mount i. Here're some observations:
* (1) "Seeing" is a mutual relationship, i.e. a can see b iff b can see a;
* (2) Lines sm_1, sm_2, sm_3, ..., sm_l have non-decreasing slope;
* (3) None of m_1, m_2, ..., m_l can be chosen;
* (4) Similarly, after neglecting m_1, ..., m_l, mountains [i+1,j] are partitioned
* into several contigous segments. We claim that each segment is independent
* of each other, which means that each segment adds a dp ans of a smaller range
* to the dp val of [i,j]. This is true because of [Lemma].
* [Lemma] If mountain a can see mountain b and c, and a cannot see any mountains
* between b, c (a is to the left of b and b is to the left of c), then a
* cannot see any mountains between b and c.
* Note that the statement implys that from mountain a, b and c are "adjacent seeable
* mountains".
*
* In impl2, we slightly modify the order of doing range dp: we compute the ranges
* by sorting them inn decreasing order of i and increasing order of j. This
* simplifies the process of finding "seeable" mountains.
*
* Time Complexity: O(n^3) (Full solution)
* Implementation 2 (Range dp)
*/
#include <bits/stdc++.h>
#include "mountains.h"
typedef std::vector<int> vec;
typedef std::complex<int> complex_t;
typedef long long ll;
const int INF = 2 * 1e9;
inline ll cross(const complex_t& c1, const complex_t& c2) {
return ll(c1.real()) * ll(c2.imag()) - ll(c1.imag()) * ll(c2.real());
}
int maximum_deevs(vec values) {
int n = values.size();
std::vector<complex_t> mounts(n);
for (int k = 0; k < n; k++)
mounts[k] = complex_t{k, values[k]};
std::vector<vec> max_d(n, vec(n));
for (int i = n - 1; i >= 0; i--) {
vec seen; // the mountains that can be seen by i and are on its right
complex_t last_vec = {0, -INF};
int prefix_sum = 0;
for (int j = i; j < n; j++) {
if (cross(mounts[j] - mounts[i], last_vec) <= 0LL) {
if (!seen.empty() && seen.back() + 1 <= j - 1)
prefix_sum += max_d[seen.back() + 1][j - 1];
seen.push_back(j);
last_vec = mounts[j] - mounts[i];
}
max_d[i][j] = 1 + prefix_sum;
if (seen.back() + 1 <= j)
max_d[i][j] += max_d[seen.back() + 1][j];
if (i < j)
max_d[i][j] = std::max(max_d[i][j], max_d[i + 1][j]); // skip i
}
}
return max_d[0][n - 1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
248 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
248 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
2 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
1 ms |
596 KB |
Output is correct |
48 |
Correct |
1 ms |
596 KB |
Output is correct |
49 |
Correct |
1 ms |
596 KB |
Output is correct |
50 |
Correct |
1 ms |
596 KB |
Output is correct |
51 |
Correct |
1 ms |
596 KB |
Output is correct |
52 |
Correct |
1 ms |
596 KB |
Output is correct |
53 |
Correct |
1 ms |
596 KB |
Output is correct |
54 |
Correct |
1 ms |
596 KB |
Output is correct |
55 |
Correct |
1 ms |
596 KB |
Output is correct |
56 |
Correct |
2 ms |
552 KB |
Output is correct |
57 |
Correct |
1 ms |
596 KB |
Output is correct |
58 |
Correct |
1 ms |
596 KB |
Output is correct |
59 |
Correct |
2 ms |
596 KB |
Output is correct |
60 |
Correct |
1 ms |
596 KB |
Output is correct |
61 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
212 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
248 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
1 ms |
596 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
596 KB |
Output is correct |
43 |
Correct |
1 ms |
596 KB |
Output is correct |
44 |
Correct |
2 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
1 ms |
596 KB |
Output is correct |
48 |
Correct |
1 ms |
596 KB |
Output is correct |
49 |
Correct |
1 ms |
596 KB |
Output is correct |
50 |
Correct |
1 ms |
596 KB |
Output is correct |
51 |
Correct |
1 ms |
596 KB |
Output is correct |
52 |
Correct |
1 ms |
596 KB |
Output is correct |
53 |
Correct |
1 ms |
596 KB |
Output is correct |
54 |
Correct |
1 ms |
596 KB |
Output is correct |
55 |
Correct |
1 ms |
596 KB |
Output is correct |
56 |
Correct |
2 ms |
552 KB |
Output is correct |
57 |
Correct |
1 ms |
596 KB |
Output is correct |
58 |
Correct |
1 ms |
596 KB |
Output is correct |
59 |
Correct |
2 ms |
596 KB |
Output is correct |
60 |
Correct |
1 ms |
596 KB |
Output is correct |
61 |
Correct |
1 ms |
596 KB |
Output is correct |
62 |
Correct |
8 ms |
4180 KB |
Output is correct |
63 |
Correct |
32 ms |
15960 KB |
Output is correct |
64 |
Correct |
32 ms |
16076 KB |
Output is correct |
65 |
Correct |
31 ms |
16084 KB |
Output is correct |
66 |
Correct |
31 ms |
16084 KB |
Output is correct |
67 |
Correct |
31 ms |
16076 KB |
Output is correct |
68 |
Correct |
30 ms |
16080 KB |
Output is correct |
69 |
Correct |
30 ms |
16016 KB |
Output is correct |
70 |
Correct |
31 ms |
16084 KB |
Output is correct |
71 |
Correct |
28 ms |
16084 KB |
Output is correct |
72 |
Correct |
31 ms |
16060 KB |
Output is correct |
73 |
Correct |
31 ms |
16084 KB |
Output is correct |
74 |
Correct |
30 ms |
16080 KB |
Output is correct |
75 |
Correct |
32 ms |
16084 KB |
Output is correct |
76 |
Correct |
30 ms |
15956 KB |
Output is correct |
77 |
Correct |
27 ms |
16084 KB |
Output is correct |
78 |
Correct |
27 ms |
16084 KB |
Output is correct |
79 |
Correct |
28 ms |
16084 KB |
Output is correct |
80 |
Correct |
28 ms |
16088 KB |
Output is correct |
81 |
Correct |
28 ms |
16088 KB |
Output is correct |
82 |
Correct |
32 ms |
16084 KB |
Output is correct |
83 |
Correct |
31 ms |
16040 KB |
Output is correct |
84 |
Correct |
30 ms |
15956 KB |
Output is correct |
85 |
Correct |
31 ms |
16072 KB |
Output is correct |
86 |
Correct |
28 ms |
16092 KB |
Output is correct |