#include "mountains.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=2e3+7;
pair<int,int>ma1[LIM][LIM], ma2[LIM][LIM];
ll T[LIM];
int dp[LIM][LIM], odw[LIM][LIM], n;
int ilo(int a, int b, int c) {
pair<ll,ll>x={b-a, T[b]-T[a]}, y={c-a, T[c]-T[a]};
return x.st*y.nd-x.nd*y.st;
}
int solve(int l, int r) {
if(l>r) return 0;
if(odw[l][r]) return dp[l][r];
odw[l][r]=1;
if(ma1[l][r].nd!=-ma2[l][r].nd) {
dp[l][r]=max(solve(l, -ma2[l][r].nd-1)+solve(-ma2[l][r].nd+1, r), solve(l, ma1[l][r].nd-1)+solve(ma1[l][r].nd+1, r));
return dp[l][r];
}
int x=ma1[l][r].nd;
dp[l][r]=solve(l, x-1)+solve(x+1, r);
int sum=1, lst=x+1;
for(int i=x+2; i<=r; ++i) {
if(ilo(x, lst, i)>=0) {
sum+=solve(lst+1, i-1);
lst=i;
}
}
lst=x-1;
for(int i=x-2; i>=l; --i) {
if(ilo(x, lst, i)<=0) {
sum+=solve(i+1, lst-1);
lst=i;
}
}
dp[l][r]=max(dp[l][r], sum);
return dp[l][r];
}
int maximum_deevs(vector<int>_y) {
n=_y.size();
rep(i, n) T[i]=_y[i];
rep(i, n) {
ma1[i][i]={T[i], i};
ma2[i][i]={T[i], -i};
for(int j=i+1; j<n; ++j) {
ma1[i][j]=max(ma1[i][j-1], {T[j], j});
ma2[i][j]=max(ma2[i][j-1], {T[j], -j});
}
}
return solve(0, n-1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Incorrect |
0 ms |
2396 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |