Optimization slope DP (study notes)

Worship summary yyb Gangster

Because yyb Gangster above the slope of the optimization of the board made it very clear, so I will not repeat them. Templates will look at several sets of questions it.

The blue book and a chapter is to raise through "task schedule" This question is the example to explain the progressive layers slope optimization.

Los weakened version of the goo, not able to live the slope optimization

Normal Edition on POJ: slope optimization template

Enhanced version of the BZOJ: slope optimization + half

Meaning of the questions: N tasks, each task has a completion time required \ (t_i \) , try these N tasks divided into several batches, each batch before the task begins, the machine need to start time S, these tasks are completed the time required for each task takes time sum (the same number of tasks to be completed at the same time). cost per task completion time is its cost multiplied by a coefficient \ (C_i \) . find the minimum total cost.

Set \ (f [i] \) represents the front i batch job into several batches of the minimum cost of the use of "advance fee calculation," the idea has the following transfer equation:

\(f[i]=min_{0<=j<i}f[j]+sumt[i]*(sumc[i]-sumc[j])+S*(sumc[N]-sumc[j])\)

Luo had only goo on the \ (N ^ 2 \) practices:

const int N=5005;
int sumt[N],sumc[N],f[N];
int main(){
    int n=read(),S=read();
    for(int i=1;i<=n;i++){
        int t=read(),c=read();
        sumt[i]=sumt[i-1]+t;
        sumc[i]=sumc[i-1]+c;
    }
    memset(f,0x3f,sizeof(f));f[0]=0;
    for(int i=1;i<=n;i++)
        for(int j=0;j<i;j++)
            f[i]=min(f[i],f[j]+sumt[i]*(sumc[i]-sumc[j])+S*(sumc[n]-sumc[j]));
    printf("%d\n",f[n]);
    return 0;
}

The POJ \ (N ^ 2 \) approach can not be passed, considering transfer type optimization. \ (F [I] = min_ {0 <= J <I} F [J] + SUMT [I] * (sumc [I ] -sumc [j]) + S * (sumc [N] -sumc [j]) \)

Set \ (K <j \) , and better than j k, i.e.,

\(f[j]+sumt[i]*(sumc[i]-sumc[j])+S*(sumc[N]-sumc[j])<f[k]+sumt[i]*(sumc[i]-sumc[k])+S*(sumc[N]-sumc[k])\)

Fucks it to get this formula,

\(\frac {f[j]-f[k]}{sumc[j]-sumc[k]}<S+sumt[i]\)

POJ been able to practice \ (O (N) \) :

const int N=300005;
int sumt[N],sumc[N],q[N],f[N];
int main(){
    int n=read(),S=read();
    for(int i=1;i<=n;i++){
        int t=read(),c=read();
        sumt[i]=sumt[i-1]+t;
        sumc[i]=sumc[i-1]+c;
    }
    memset(f,0x3f,sizeof(f));f[0]=0;
    int l=1,r=1;q[1]=0;
    for(int i=1;i<=n;i++){
        while(l<r&&(f[q[l+1]]-f[q[l]])<=(S+sumt[i])*(sumc[q[l+1]]-sumc[q[l]]))l++;
        f[i]=f[q[l]]+(sumt[i]*sumc[i]+S*sumc[n])-(S+sumt[i])*sumc[q[l]];
        while(l<r&&(f[q[r]]-f[q[r-1]])*(sumc[i]-sumc[q[r]])>=(f[i]-f[q[r]])*(sumc[q[r]]-sumc[q[r-1]]))r--;
        q[++r]=i;
    }
    printf("%d\n",f[n]);
    return 0;
}

Topic strengthened: the execution time of a task \ (t_i \) may be a negative number, indicating that \ (sumt [i] \) is no longer guaranteed monotonic, so we have to maintain the entire cohort, the first team is not necessarily the best, every time required when transferring binary search, to find a position j, j is less than the slope of the left segment \ (S + sumt [i] \) smaller than the slope of the right segment j \ (S + sumt [i] \) large

You can live BZOJ approach:

const int N=300005;
LL sumt[N],sumc[N],q[N],f[N];
inline int erfen(int i,int j,int l,int r){
    if(l==r)return q[l];
    while(l<r){
        int mid=(l+r)>>1;
        if(f[q[mid+1]]-f[q[mid]]<=j*(sumc[q[mid+1]]-sumc[q[mid]]))l=mid+1;
        else r=mid;
    }
    return q[l];
}
int main(){
    int n=read(),S=read();
    for(int i=1;i<=n;i++){
        int t=read(),c=read();
        sumt[i]=sumt[i-1]+t;
        sumc[i]=sumc[i-1]+c;
    }
    int l=1,r=1;
    for(int i=1;i<=n;i++){
        int j=erfen(i,S+sumt[i],l,r);
        f[i]=f[j]+(sumt[i]*sumc[i]+S*sumc[n])-(S+sumt[i])*sumc[j];
        while(l<r&&(f[q[r]]-f[q[r-1]])*(sumc[i]-sumc[q[r]])>=(f[i]-f[q[r]])*(sumc[q[r]]-sumc[q[r-1]]))r--;
        q[++r]=i;
    }
    printf("%lld\n",f[n]);
    return 0;
}

Individual optimization of the slope some understanding: First of all pre-knowledge --- monotonous queue must master and secondly because, after all, is one of the optimization method of DP, to get the title, we must first set a good state, listed in transition equation (slope optimization. entitled, \ (O (n-2 ^) \) of the transfer equation are generally readily available).

After finishing consider the transfer equation to get this formula, and are generally required to use a prefix, then a common routine is yyb chiefs also referred to the "Setting k <j and j better than k", then you can get on a j and k with a constant inequality, then the slope can be optimized.

Then some of the details need to understand their own, such as maintaining the monotony of the queue is increasing or decreasing? Prefix and whether to open long long? Maintenance time will certainly want to compare the size of most people usually write a function with a double, but I personally write the following "Print Article" double life difficult time with this question, and replaced with long long pass by, like this comparison metaphysical things, on the fight character, big deal one by one try again.

[HNOI2008] packing toys TOY

[APIO2010] Einsatzgruppen

Print Article

[CEOI2004] sawmill site

[ZJOI2007] warehouse construction

Guess you like

Origin www.cnblogs.com/PPXppx/p/11007448.html