CodeForces - 311B:Cats Transport (DP+斜率優化)
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
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.
Input
The first line of the input contains three integers n, m, p (2 ≤ n ≤ 105, 1 ≤ m ≤ 105, 1 ≤ p ≤ 100).
The second line contains n - 1 positive integers d2, d3, ..., dn (1 ≤ di < 104).
Each of the next m lines contains two integers hi and ti (1 ≤ hi ≤ n, 0 ≤ ti ≤ 109).
Output
Output an integer, the minimum sum of waiting time of all cats.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Examples
Input4 6 2Output
1 3 5
1 0
2 1
4 9
1 10
2 10
3 12
3
題意:有一些貓,放在一些位置,人走到每個貓的時間已知,給個貓出現的時間已知,假設派出一個人,可以自由安排其出發時間,沿途已經出現的貓pick掉,貓等待的時間是被pick的時間減去出現的時間t,t>=0。現在有P個人,問總時間T最小是多少。
思路:對貓: 人time+貓dis-貓time。把c[i]-t[i]排序,那麽就成為了把M個數劃分位P個區間,每個區間的值=所有數與最大數的差值。
DP[i][j]=min DP[k][j-1]+c[i]*(i-k)-(sum[i]-sum[k]);
轉化:B=-c[i]*k+(dp[k][j-1]+sum[k])+c[i]*i-sum[i];
方程的斜率為k=c[i];y= (dp[k][j-1]+sum[k]) ;截距B=DP[i][j];常數C=c[i]*i-sum[i];
#include<bits/stdc++.h> #define ll long long using namespace std; const int maxn=100010; ll d[maxn],c[maxn],sum[maxn],dp[maxn][101],t; int q[maxn],head,tail; ll getans(int i,int j,int k){ return dp[k][j-1]+c[i]*(i-k)-(sum[i]-sum[k]); } ll Y(int k,int j){ return dp[k][j-1]+sum[k]; } int main() { int N,M,P,i,j,h; scanf("%d%d%d",&N,&M,&P); for(i=2;i<=N;i++) scanf("%I64d",&d[i]),d[i]+=d[i-1]; for(i=1;i<=M;i++){ scanf("%d%I64d",&h,&t); c[i]=t-d[h]; } sort(c+1,c+M+1); for(i=1;i<=M;i++) sum[i]=sum[i-1]+c[i]; for(i=1;i<=M;i++) dp[i][1]=c[i]*(i-1)-sum[i-1]; for(j=2;j<=P;j++){ head=tail=0; for(i=1;i<=M;i++){ while(tail>head&&Y(q[head+1],j)-Y(q[head],j)<c[i]*(q[head+1]-q[head])) head++; dp[i][j]=getans(i,j,q[head]); while(tail>head&&(Y(i,j)-Y(q[tail],j))*(q[tail]-q[tail-1])<(Y(q[tail],j)-Y(q[tail-1],j))*(i-q[tail])) tail--; q[++tail]=i; } } printf("%I64d\n",dp[M][P]); return 0; }
經驗:彈出隊首時,可以直接通過比較結果獲得。
#include<bits/stdc++.h> #define ll long long using namespace std; const int maxn=100011; ll d[maxn],c[maxn],sum[maxn],dp[maxn][101],t; int q[maxn],head,tail; ll getans(int i,int j,int k){ return dp[k][j-1]+c[i]*(i-k)-(sum[i]-sum[k]); } ll Y(int k,int j){ return dp[k][j-1]+sum[k]; } int main() { int N,M,P,i,j,h; scanf("%d%d%d",&N,&M,&P); for(i=2;i<=N;i++) scanf("%I64d",&d[i]),d[i]+=d[i-1]; for(i=1;i<=M;i++){ scanf("%d%I64d",&h,&t); c[i]=t-d[h]; } sort(c+1,c+M+1); for(i=1;i<=M;i++) sum[i]=sum[i-1]+c[i]; for(i=1;i<=M;i++) dp[i][1]=c[i]*(i-1)-sum[i-1]; for(j=2;j<=P;j++){ head=tail=0; for(i=1;i<=M;i++){ while(tail>head&&getans(i,j,q[head])>getans(i,j,q[head+1])) head++; dp[i][j]=getans(i,j,q[head]); while(tail>head&&(Y(i,j)-Y(q[tail],j))*(q[tail]-q[tail-1])<(Y(q[tail],j)-Y(q[tail-1],j))*(i-q[tail])) tail--; q[++tail]=i; //隊首可以getans維護,隊尾不行,必須維護斜率! } } printf("%I64d\n",dp[M][P]); return 0; }View Code
CodeForces - 311B:Cats Transport (DP+斜率優化)