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.
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
Input
4 6 21 3 51 02 14 91 102 103 12
Output
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; }
原文地址:https://www.cnblogs.com/hua-dong/p/9246212.html