#include "mountains.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define xx first
#define yy second
#define all(v) (v).begin(), (v).end()
// * dp[l][r] : r을 반드시 포함하여 l~r에서 지을 수 있는 최대 개수
// k[] = r에서 보이는 점들의 인덱스들을 순서대로 쓴 배열.
// -> dp[l][r]은 k를 기준으로 partition을 나눠서, 각 최적을 합한 것과 같다.
// k[i] = k[]에서 l의 lower bound.
// opt(l ~ r) = 구간 [l~r]에서 최적 = max{l <= i <= r}(dp[l][i])
// dp[l][r] = opt(l ~ k[i]-1) + opt(k[i]+1, k[i+1]-1) + ...+ opt(k[-2]+1, k[-1]-1) + 1
int n, dp[2020][2020], opt[2020][2020];
ll h[2020];
int get_opt(int l, int r) {
auto &ret = opt[l][r];
if(ret != -1) return ret;
assert(dp[l][r] != -1); assert(dp[l][r] <= r-l+1);
ret = dp[l][r];
if(l <= r-1) ret = max(ret, get_opt(l, r-1));
return ret;
}
bool canSee(int l, int mid, int r) {
return (mid - l)*(h[r] - h[l]) - (r - l)*(h[mid] - h[l]) >= 0;
}
int maximum_deevs(vector<int> _y){
n = _y.size();
for(int i=1; i<=n; ++i) h[i] = _y[i-1];
for(int i=0; i<=n+1; ++i)
for(int j=0; j<=n+1; ++j)
dp[i][j] = opt[i][j] = -1;
dp[1][1] = 1;
for(int r=2; r<=n; ++r) {
vector<int> ks = {r-1};
for(int l=r-2; l>=1; --l)
if(canSee(l, ks.back(), r))
ks.push_back(l);
ks.push_back(0);
dp[r][r] = dp[r-1][r] = 1;
int opt_sum = 1;
// cout << "*** " << r << " : "; cout << endl;
for(int ki=1; ki<ks.size(); ++ki) {
int k2 = ks[ki-1], k1 = ks[ki];
for(int l=k1+1; l<k2; ++l)
dp[l][r] = opt_sum + get_opt(l, k2-1);
if(k1+1 <= k2-1)
opt_sum += get_opt(k1+1, k2-1);
dp[k1][r] = opt_sum;
// cout << k1 << '~' << k2 << " : ";
// for(int i=k1; i<k2; ++i) cout << dp[i][r] << ' '; cout << endl;
}
}
return get_opt(1, n);
}
Compilation message
mountains.cpp: In function 'int maximum_deevs(std::vector<int>)':
mountains.cpp:53:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int ki=1; ki<ks.size(); ++ki) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
440 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
440 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
572 KB |
Output is correct |
21 |
Correct |
1 ms |
568 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
568 KB |
Output is correct |
24 |
Correct |
1 ms |
576 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
568 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
572 KB |
Output is correct |
34 |
Correct |
1 ms |
564 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
572 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
440 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
572 KB |
Output is correct |
21 |
Correct |
1 ms |
568 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
568 KB |
Output is correct |
24 |
Correct |
1 ms |
576 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
568 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
572 KB |
Output is correct |
34 |
Correct |
1 ms |
564 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
572 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
2260 KB |
Output is correct |
40 |
Correct |
2 ms |
3412 KB |
Output is correct |
41 |
Correct |
2 ms |
3412 KB |
Output is correct |
42 |
Correct |
2 ms |
3412 KB |
Output is correct |
43 |
Correct |
2 ms |
3412 KB |
Output is correct |
44 |
Correct |
2 ms |
3412 KB |
Output is correct |
45 |
Correct |
3 ms |
3412 KB |
Output is correct |
46 |
Correct |
3 ms |
3412 KB |
Output is correct |
47 |
Correct |
2 ms |
3412 KB |
Output is correct |
48 |
Correct |
2 ms |
3412 KB |
Output is correct |
49 |
Correct |
3 ms |
3388 KB |
Output is correct |
50 |
Correct |
2 ms |
3412 KB |
Output is correct |
51 |
Correct |
2 ms |
3412 KB |
Output is correct |
52 |
Correct |
3 ms |
3412 KB |
Output is correct |
53 |
Correct |
2 ms |
3412 KB |
Output is correct |
54 |
Correct |
2 ms |
3388 KB |
Output is correct |
55 |
Correct |
2 ms |
3412 KB |
Output is correct |
56 |
Correct |
3 ms |
3412 KB |
Output is correct |
57 |
Correct |
2 ms |
3412 KB |
Output is correct |
58 |
Correct |
2 ms |
3412 KB |
Output is correct |
59 |
Correct |
2 ms |
3412 KB |
Output is correct |
60 |
Correct |
2 ms |
3412 KB |
Output is correct |
61 |
Correct |
2 ms |
3384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
440 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
572 KB |
Output is correct |
21 |
Correct |
1 ms |
568 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
568 KB |
Output is correct |
24 |
Correct |
1 ms |
576 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
568 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
572 KB |
Output is correct |
34 |
Correct |
1 ms |
564 KB |
Output is correct |
35 |
Correct |
1 ms |
596 KB |
Output is correct |
36 |
Correct |
1 ms |
572 KB |
Output is correct |
37 |
Correct |
1 ms |
596 KB |
Output is correct |
38 |
Correct |
1 ms |
596 KB |
Output is correct |
39 |
Correct |
1 ms |
2260 KB |
Output is correct |
40 |
Correct |
2 ms |
3412 KB |
Output is correct |
41 |
Correct |
2 ms |
3412 KB |
Output is correct |
42 |
Correct |
2 ms |
3412 KB |
Output is correct |
43 |
Correct |
2 ms |
3412 KB |
Output is correct |
44 |
Correct |
2 ms |
3412 KB |
Output is correct |
45 |
Correct |
3 ms |
3412 KB |
Output is correct |
46 |
Correct |
3 ms |
3412 KB |
Output is correct |
47 |
Correct |
2 ms |
3412 KB |
Output is correct |
48 |
Correct |
2 ms |
3412 KB |
Output is correct |
49 |
Correct |
3 ms |
3388 KB |
Output is correct |
50 |
Correct |
2 ms |
3412 KB |
Output is correct |
51 |
Correct |
2 ms |
3412 KB |
Output is correct |
52 |
Correct |
3 ms |
3412 KB |
Output is correct |
53 |
Correct |
2 ms |
3412 KB |
Output is correct |
54 |
Correct |
2 ms |
3388 KB |
Output is correct |
55 |
Correct |
2 ms |
3412 KB |
Output is correct |
56 |
Correct |
3 ms |
3412 KB |
Output is correct |
57 |
Correct |
2 ms |
3412 KB |
Output is correct |
58 |
Correct |
2 ms |
3412 KB |
Output is correct |
59 |
Correct |
2 ms |
3412 KB |
Output is correct |
60 |
Correct |
2 ms |
3412 KB |
Output is correct |
61 |
Correct |
2 ms |
3384 KB |
Output is correct |
62 |
Correct |
14 ms |
16172 KB |
Output is correct |
63 |
Correct |
55 ms |
31944 KB |
Output is correct |
64 |
Correct |
56 ms |
31956 KB |
Output is correct |
65 |
Correct |
46 ms |
31996 KB |
Output is correct |
66 |
Correct |
48 ms |
32000 KB |
Output is correct |
67 |
Correct |
49 ms |
31992 KB |
Output is correct |
68 |
Correct |
52 ms |
32076 KB |
Output is correct |
69 |
Correct |
52 ms |
31996 KB |
Output is correct |
70 |
Correct |
51 ms |
31956 KB |
Output is correct |
71 |
Correct |
50 ms |
32016 KB |
Output is correct |
72 |
Correct |
52 ms |
31956 KB |
Output is correct |
73 |
Correct |
49 ms |
32016 KB |
Output is correct |
74 |
Correct |
50 ms |
31956 KB |
Output is correct |
75 |
Correct |
48 ms |
32016 KB |
Output is correct |
76 |
Correct |
54 ms |
31984 KB |
Output is correct |
77 |
Correct |
50 ms |
32048 KB |
Output is correct |
78 |
Correct |
48 ms |
32048 KB |
Output is correct |
79 |
Correct |
49 ms |
32056 KB |
Output is correct |
80 |
Correct |
48 ms |
31956 KB |
Output is correct |
81 |
Correct |
53 ms |
32028 KB |
Output is correct |
82 |
Correct |
47 ms |
31992 KB |
Output is correct |
83 |
Correct |
56 ms |
32076 KB |
Output is correct |
84 |
Correct |
59 ms |
31892 KB |
Output is correct |
85 |
Correct |
53 ms |
32000 KB |
Output is correct |
86 |
Correct |
55 ms |
32000 KB |
Output is correct |