(中等) CF 311B Cats Transport,斜率最佳化DP。

來源:互聯網
上載者:User

標籤:

  Zxr960115 is owner of a large farm. He feeds m cute cats and employs p feeders. There‘s a straight road across the farm and n hills along the road, numbered from 1 to n from left to right. The distance between hill i and (i - 1) is di meters. The feeders live in hill 1.

One day, the cats went out to play. Cat i went on a trip to hill hi, finished its trip at time ti, and then waited at hill hi for a feeder. The feeders must take all the cats. Each feeder goes straightly from hill 1 to n without waiting at a hill and takes all the waiting cats at each hill away. Feeders walk at a speed of 1 meter per unit time and are strong enough to take as many cats as they want.

For example, suppose we have two hills (d2 = 1) and one cat that finished its trip at time 3 at hill 2 (h1 = 2). Then if the feeder leaves hill 1 at time 2 or at time 3, he can take this cat, but if he leaves hill 1 at time 1 he can‘t take it. If the feeder leaves hill 1 at time 2, the cat waits him for 0 time units, if the feeder leaves hill 1 at time 3, the cat waits him for 1 time units.

Your task is to schedule the time leaving from hill 1 for each feeder so that the sum of the waiting time of all cats is minimized.

 

  隊伍訓練時做到的題目,好不容易推出了dp公式並且想到了斜率最佳化,結果犯了SB錯誤導致一直錯。。。

  題目大致就是幾隻貓在一些地方,然後讓人去收貓。。。餓還是看題吧。。。

  直接對於某個位置的貓的 t 減去到這個位置的時間就好,問題就轉換成了有一些貓每個貓都有一個值,然後給P個人分別一個值,然後每個貓的找到比他大的最近的那個人的值,然後相減,累加每個貓的,讓總和最小。。。餓,表達稍微比較爛。。。

  這裡考慮到每個人的值一定是某隻貓的值H,不然向下移動一點點可以更優。

  對每個貓的值進行排序,然後從左至右dp,

  dp[i][j]表示前i個貓,使用了j個人,的最小值。。。

  然後遞推的話 dp[i][j]=min{ dp[x][j-1]+(i-x)H[i]-S[i]+S[x] };

  S表示H的首碼和。

  然後轉換一下變成 min{ dp[x][j-1]+S[x]-xH[i] } + iH[i]-S[i];

  然後 Y[x]=dp[x][j-1] ,X[x]=x;

  然後就是經典斜率DP的問題了。。。

代碼如下:

// ━━━━━━神獸出沒━━━━━━//      ┏┓       ┏┓//     ┏┛┻━━━━━━━┛┻┓//     ┃           ┃//     ┃     ━     ┃//     ████━████   ┃//     ┃           ┃//     ┃    ┻      ┃//     ┃           ┃//     ┗━┓       ┏━┛//       ┃       ┃//       ┃       ┃//       ┃       ┗━━━┓//       ┃           ┣┓//       ┃           ┏┛//       ┗┓┓┏━━━━━┳┓┏┛//        ┃┫┫     ┃┫┫//        ┗┻┛     ┗┻┛//// ━━━━━━感覺萌萌噠━━━━━━// Author        : WhyWhy// Created Time  : 2015年10月09日 星期五 18時45分52秒// File Name     : B.cpp#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;const int MaxN=100005;int N,M,P;long long DP1[MaxN],DP2[MaxN];long long *dp1,*dp2;long long H[MaxN];long long S[MaxN];long long X[MaxN],Y[MaxN],cou;long long d[MaxN];bool better(int a,int b,long long H){    return (Y[a]-X[a]*H)<=(Y[b]-X[b]*H);}bool judge(long long x1,long long y1,long long x2,long long y2,long long x3,long long y3){    return (y1-y2)*(x2-x3)<=(y2-y3)*(x1-x2);}int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    ios::sync_with_stdio(false);    cin>>N>>M>>P;    d[1]=0;    for(int i=2;i<=N;++i)    {        cin>>d[i];        d[i]+=d[i-1];    }    long long a,b;    for(int i=1;i<=M;++i)    {        cin>>a>>b;        H[i]=b-d[a];    }    dp1=DP1;    dp2=DP2;    N=M;    sort(H+1,H+N+1);    S[0]=0;    for(int i=1;i<=N;++i)    {        S[i]=S[i-1]+H[i];        dp1[i]=H[i]*i-S[i];    }    int p;    long long TX,TY;    P=min(P,N);    for(int j=2;j<=P;++j)    {        cou=1;        Y[0]=dp1[j-1]+S[j-1];        X[0]=j-1;        p=0;        for(int i=j;i<=N;++i)        {            while(p<cou-1 && better(p+1,p,H[i])) ++p;            dp2[i]=Y[p]-X[p]*H[i]-S[i]+i*H[i];            TX=i;            TY=dp1[i]+S[i];            while(cou-1>p && judge(TX,TY,X[cou-1],Y[cou-1],X[cou-2],Y[cou-2])) --cou;            X[cou]=TX;            Y[cou++]=TY;        }        swap(dp1,dp2);    }    cout<<dp1[N]<<endl;    return 0;}
View Code

 

(中等) CF 311B Cats Transport,斜率最佳化DP。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.