[Dp] a question decisions monotonic slope optimization optimization dp

luoguP3515

Given a sequence of length n, a1, a2, ..., an.
For each 1 <= i <= n, find the smallest non-negative integer p satisfying:
For any J, \ (a_j \) <= \ (a_i \) + p- \ (\ sqrt {| ij of |} \ )
n-<=. 5 * \ (10. 5 ^ \)

Consider j <i in terms
need to meet the j <IA [J] <= A [i] + p- \ (\} ij of sqrt {\) , namely: A [J] + \ (\ sqrt {} ij of \ ) <= a [I] + P
provided g is a decision point that is a [g] + \ (\ IG sqrt {} \) <= a [I] + g is determined that P \ (ans_p \) that g
Therefore, there is for any positive integer k a [GK] + \ (\ sqrt {I- (GK)} \) <a = [G] + \ (\ IG sqrt {} \)
Therefore:
so for interrogation point i + 1
has: A [GK] + \ (\ I + sqrt {l- (GK)} \) <A [G] + \ (\ + sqrt {I-1} G \) [1]
as: \ (\ l- {I + sqrt (GK)} \) - \ (\ sqrt (I- (GK)) \) < \ (\ sqrt {I-G. 1} + \) - \ (\ IG sqrt {} \)
because: for sqrt function, the greater the value the smaller the slope, the greater the value that is smaller by a function of increasing the value of variable +1

For [1] conclusions: All gk I points are not i + decision point 1 (as '<', it certainly i + 1 g When dot ratio gk preferably)
so that under the conditions of j <i for the i + after the decision point position 1 is always the decision point in the i

For j> i empathy.
In such \ (\ mathbf {} decision monotonicity \) in the case, can be bipartite + dp.

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int maxn=5e5+50;

//决策单调性优化dp 斜率优化

int a[maxn];
double p[maxn][2];
inline void solve(int l,int r,int L,int R,bool ju)
{
    if(l>r)return;
    int mid=(l+r)>>1,id=mid;
    for(int i=L;i<=min(mid,R);i++)
    {
        if(a[i]>1.0*a[mid]+1.0*p[mid][ju]-sqrt(abs(mid-i)))
        {
            p[mid][ju]=1.0*a[i]-1.0*a[mid]+sqrt(abs(mid-i));
            id=i;
        }
    }
    solve(l,mid-1,L,id,ju);solve(mid+1,r,id,R,ju);
}


int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    solve(1,n,1,n,0);
    for(int i=1;i<=n/2;i++)swap(a[i],a[n+1-i]);
    solve(1,n,1,n,1);
    for(int i=1;i<=n;i++)
        printf("%.0f\n",ceil(max(p[n-i+1][1],p[i][0])));
}

Guess you like

Origin www.cnblogs.com/kkkek/p/11997355.html