bzoj2216
决策单调性+整体二分
这里就是j<k且kj劣于j,j不会再选,所以我们整体二分
pos是因为从L->R中这个是最优点,所以对于mid+1->r选pos之前肯定不优,l->mid-1不会选>pos,因为每个位置都小于mid,并且pos->mid-1这段区间的决策点没有pos优,因为当前f[i]的i小于mid,选的决策的位置大于pos,由于i小于mid,所以sqrt(i-j),j越大,下降越快,所以pos+1->mid-1肯定没pos优
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; const int N = 500010; int n; int a[N], id[N]; double f1[N], f2[N]; double calc(int i, int j) { return (double)a[j] - (double)a[i] + sqrt(abs((double)i - (double)j)); } void solve(int l, int r, int L, int R, double *f) { if(l > r) return; int mid = (l + r) >> 1, lim = min(mid, R), pos = lim; double mx = 0; for(int i = L; i <= lim; ++i) if(calc(mid, i) > mx) { mx = calc(mid, i); pos = i; } f[id[mid]] = mx; solve(l, mid - 1, L, pos, f); solve(mid + 1, r, pos, R, f); } int main() { scanf("%d", &n); for(int i = 1; i <= n; ++i) { id[i] = i; scanf("%d", &a[i]); } solve(1, n, 1, n, f1); reverse(a + 1, a + n + 1); reverse(id + 1, id + n + 1); solve(1, n, 1, n, f2); for(int i = 1; i <= n; ++i) printf("%d\n", (int)ceil(max(f1[i], f2[i]))); return 0; }