Mountains
题意:
选最多的点使得两两看不见。
分析:
分治,solve(l,r)为区间[l,r]的答案。那么如果不选最高点,分治两边即可,选了最高点,那么在最高点看不见的区间里分治。
代码:
#include"mountains.h"
#include<bits/stdc++.h>
using namespace std; const int N = ;
int y[N];
int f[N][N]; bool Judge(int a,int b,int c) { // a see c ?
// ((y[a] - y[b]) / (a - b)) > ((y[a] - y[c]) / (a - c));
return 1ll * (y[a] - y[b]) * (a - c) > 1ll * (y[a] - y[c]) * (a - b);
} int solve(int l,int r) {
if (f[l][r]) return f[l][r];
if (l > r) return ;
if (l == r) return ;
int pos = -, mx = -;
for (int i=l; i<=r; ++i) if (y[i] > mx) pos = i, mx = y[i];
int ans1 = solve(l, pos - ) + solve(pos + , r); // select max_height_node int ans2 = ; // don't select this node
for (int i=pos-,j; i>=l; i=j) {
for (j=i-; j>=l; --j) { // i can see pos
if (Judge(j, i, pos)) { // j can't see pos
if (j == l) ans2 += solve(l, i - );
continue;
}
if (j + <= i - ) ans2 += solve(j + , i - ); // this Section can't see pos
break;
}
}
for (int i=pos+,j; i<=r; i=j) {
for (j=i+; j<=r; ++j) {
if (Judge(pos, i, j)) {
if (j == r) ans2 += solve(i + , r);
continue;
}
if (i + <= j - ) ans2 += solve(i + , j - );
break;
}
}
return f[l][r] = max(ans1, ans2 + );
} int maximum_deevs(vector<int> A) {
int n = A.size();
for (int i=; i<=n; ++i) y[i] = A[i - ];
return solve(, n);
}