CF311B Cats Transport [solution] slope optimization problem

Questions surface: http://codeforces.com/contest/311/problem/B

Luogu questions surface: https://www.luogu.com.cn/problem/CF311B

Optimization slope DP

We set t [i] = the end time of kittens - from 1 to place it in the distance.

Since when is starting to let the cat can not wait.

To the best trip we definitely do not want a wait.

His sort, then that can be received on a k cat breeder picked up after all this time just to receive between cats cat i.

Around a bit.

Then dp formula can be introduced.

f [i] [j] = min (f [i] [j], f [i -1] [k] + T [j] * (jk) - (S [j] -s [k])).

Removed min.

f[i-1][k]+s[k]=t[j]*k+f[i][j]-t[j]*j+s[j]

Set to y = kx + b form.

Then k is t [j], x is k, y is in front of that a lot.

You will be happy to maintain the lower slope of the convex hull is optimized.

code show as below:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+10;
ll f[110][maxn],g[maxn],s[maxn],t[maxn];
int n,m,p,d[maxn],q[maxn];
int main()
{
    scanf("%d%d%d",&n,&m,&p);
    for(int i=2;i<=n;i++){
        scanf("%d",&d[i]);d[i]+=d[i-1];
    }
    for(int i=1;i<=m;i++){
        int x,y;scanf("%d%d",&x,&y);
        t[i]=y-d[x];
    }
    sort(t+1,t+1+m);
    for(int i=1;i<=m;i++) s[i]=s[i-1]+t[i];
    memset(f,0x3f,sizeof(f));
    f[0][0]=0;
    for(int i=1;i<=p;i++){
        for(int j=1;j<=m;j++) g[j]=f[i-1][j]+s[j];
        int l=1,r=1;q[1]=0;
        for(int j=1;j<=m;j++){
            while(l<r && g[q[l+1]]-g[q[l]]<=t[j]*(q[l+1]-q[l])) l++;
            f[i][j]=min(f[i-1][j],g[q[l]]+t[j]*(j-q[l])-s[j]);
            if(g[j]>=0x3f3f3f3f3f3f3f3fll) continue;
            while(l<r && (g[j]-g[q[r]])*(q[r]-q[r-1])<=(g[q[r]]-g[q[r-1]])*(j-q[r])) r--;
            q[++r]=j;
        }
    }
    printf("%lld\n",f[p][m]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/ChrisKKK/p/11524459.html