CodeForces 311B/CSU 1963 Cats Transport/Feed The Rabbit(斜率优化dp)
2017-07-23 09:56
447 查看
[align=center]Cats Transport[/align]
[align=center]time limit per test:2 seconds[/align]
[align=center]memory limit per test:256 megabytes[/align]
[align=center]input:standard input[/align]
[align=center]output:standard output[/align]
Zxr960115 is owner of a large farm. He feeds m cute cats and employsp feeders. There's a straight road across the farm andn hills along the
road, numbered from 1 ton from left to right. The distance between hilli and(i - 1) isdi
meters. The feeders live in hill 1.
One day, the cats went out to play. Cat i went on a trip to hillhi, finished its trip at timeti,
and then waited at hillhi for a feeder. The feeders must take all the cats. Each feeder goes straightly from hill 1 ton
without waiting at a hill and takes all thewaiting 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 integersd2, d3, ..., dn(1 ≤ di < 104).
Each of the next m lines contains two integershi andti(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 thecin,cout streams or the%I64d
specifier.
Examples
Input
Output
突然冒出来一道斜率优化的题,我都快忘了……赶紧翻原来写的博客……
题意也不是很好理解……有n座山,m只猫,每只猫都在固定一座山上,每座山线性排列,告诉你山之间的距离,猫i在某个时刻ti出现,然后总共有p个饲养员,他们从第一座山出发,单位时间走单位距离,而且都是一只往前走不停,每次路过可以把出现的猫领走,然后饲养员出发时间可以不同。问如何安排才能够使得所有猫的总等待时间最少,输出这个时间。不管你有没有题意了,没懂自己再去看题……
首先,破题关键是这个饲养员出发时间可以不同。对于某一个饲养员,他出发可以拿走的猫可以看成一个组。我们知道,如果想取走某只猫i,那么饲养员出发的时间一定的大于等于ti-di,即猫出现的时间减去到达猫所在地的时间。那么对于这个组来说,出发时间就得大于最大的ti-di的值,对应等待时间最短,故要刚好等于。知道了出发时间,那么总的等待时间,就是用这个最大的ti-di减去其他所有组内的ti-di的和,这个自己理解一下。
那么,现在问题就转化为了,把某些东西分成p个组,按照一定的权值计算方式,使得分成p个组之后的总和最小。进一步观察可以发现,我们可以令a[i]=t[i]-d[i],然后按照a[i]的值进行排序,这样分组就更加方便。排序之后,很容易写出分组的dp方程:dp[i][j]=dp[i-1][k]+sigma(a[j]-a[l])(k<l<=j)=dp[i][k]+a[j]*(j-k)-s[j]+s[k]。其中,dp[i][j]表示前j个人分成i组的最小等待时间,s[j]表示a数组的前缀和。
看到这里应该就很熟悉了,典型的斜率dp的样子,转换一下就行了。还是一样,假设决策k优于决策l(k>l),那么有dp[i-1][k]+a[j]*(j-k)-s[j]+s[k]<dp[i-1][l]+a[j]*(j-l)-s[j]+s[l],将不等式化简并移项可得(dp[i-1][k]+s[k])-(dp[i-1][l]+s[l])<a[j]*(k-l)。这样一来,就有一个斜率表达式K=((dp[i-1][k]+s[k])-(dp[i-1][l]+s[l]))/(k-l),而这个斜率要满足小于a[j],即两点的斜率小于a[j],所以我们要找到最优的k使得,他对其他所有的l的斜率都小于a[j]。注意到dp[i-1][k]+s[k]、k关于k都是个单调递增的函数,所以我们的斜率K都是大于零的。然后要斜率越来越大,所以我们维护一个向右下凸出的一系列直线轮廓,所以维护一个前后两点斜率单调递增的单调点队列,每次取满足斜率条件的最大决策,然后处理完后把该点也加入队列,维护队列单调性。单调性维护的话就是找三个点i<k<j,如果K(i,k)>K(k,j),那么显然k点没有意义,可以去掉,如此维护下去。具体见代码:
[align=center]time limit per test:2 seconds[/align]
[align=center]memory limit per test:256 megabytes[/align]
[align=center]input:standard input[/align]
[align=center]output:standard output[/align]
Zxr960115 is owner of a large farm. He feeds m cute cats and employsp feeders. There's a straight road across the farm andn hills along the
road, numbered from 1 ton from left to right. The distance between hilli and(i - 1) isdi
meters. The feeders live in hill 1.
One day, the cats went out to play. Cat i went on a trip to hillhi, finished its trip at timeti,
and then waited at hillhi for a feeder. The feeders must take all the cats. Each feeder goes straightly from hill 1 ton
without waiting at a hill and takes all thewaiting 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 integersd2, d3, ..., dn(1 ≤ di < 104).
Each of the next m lines contains two integershi andti(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 thecin,cout streams or the%I64d
specifier.
Examples
Input
4 6 2 1 3 5 1 0 2 1 4 9 1 10 2 10 3 12
Output
3
突然冒出来一道斜率优化的题,我都快忘了……赶紧翻原来写的博客……
题意也不是很好理解……有n座山,m只猫,每只猫都在固定一座山上,每座山线性排列,告诉你山之间的距离,猫i在某个时刻ti出现,然后总共有p个饲养员,他们从第一座山出发,单位时间走单位距离,而且都是一只往前走不停,每次路过可以把出现的猫领走,然后饲养员出发时间可以不同。问如何安排才能够使得所有猫的总等待时间最少,输出这个时间。不管你有没有题意了,没懂自己再去看题……
首先,破题关键是这个饲养员出发时间可以不同。对于某一个饲养员,他出发可以拿走的猫可以看成一个组。我们知道,如果想取走某只猫i,那么饲养员出发的时间一定的大于等于ti-di,即猫出现的时间减去到达猫所在地的时间。那么对于这个组来说,出发时间就得大于最大的ti-di的值,对应等待时间最短,故要刚好等于。知道了出发时间,那么总的等待时间,就是用这个最大的ti-di减去其他所有组内的ti-di的和,这个自己理解一下。
那么,现在问题就转化为了,把某些东西分成p个组,按照一定的权值计算方式,使得分成p个组之后的总和最小。进一步观察可以发现,我们可以令a[i]=t[i]-d[i],然后按照a[i]的值进行排序,这样分组就更加方便。排序之后,很容易写出分组的dp方程:dp[i][j]=dp[i-1][k]+sigma(a[j]-a[l])(k<l<=j)=dp[i][k]+a[j]*(j-k)-s[j]+s[k]。其中,dp[i][j]表示前j个人分成i组的最小等待时间,s[j]表示a数组的前缀和。
看到这里应该就很熟悉了,典型的斜率dp的样子,转换一下就行了。还是一样,假设决策k优于决策l(k>l),那么有dp[i-1][k]+a[j]*(j-k)-s[j]+s[k]<dp[i-1][l]+a[j]*(j-l)-s[j]+s[l],将不等式化简并移项可得(dp[i-1][k]+s[k])-(dp[i-1][l]+s[l])<a[j]*(k-l)。这样一来,就有一个斜率表达式K=((dp[i-1][k]+s[k])-(dp[i-1][l]+s[l]))/(k-l),而这个斜率要满足小于a[j],即两点的斜率小于a[j],所以我们要找到最优的k使得,他对其他所有的l的斜率都小于a[j]。注意到dp[i-1][k]+s[k]、k关于k都是个单调递增的函数,所以我们的斜率K都是大于零的。然后要斜率越来越大,所以我们维护一个向右下凸出的一系列直线轮廓,所以维护一个前后两点斜率单调递增的单调点队列,每次取满足斜率条件的最大决策,然后处理完后把该点也加入队列,维护队列单调性。单调性维护的话就是找三个点i<k<j,如果K(i,k)>K(k,j),那么显然k点没有意义,可以去掉,如此维护下去。具体见代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #define N 100100 #define M 111 using namespace std; long long s ,a ,q ,dp[M] ; int h,t,n,m,p,i,j,d ; long long x; double getdp(int k) { return dp[i-1][k]+a[j]*(j-k)-s[j]+s[k]; } long long Y(int k) { return dp[i-1][k]+s[k]; } long long X(int x) { return x; } bool judge(int l,int k,int j) //如果满足条件,则k比l更优 { return (Y(k)-Y(l))<=(X(k)-X(l))*a[j]; } bool maintain(int k,int i,int j) { return (Y(k)-Y(i))*(X(j)-X(k))>=(Y(j)-Y(k))*(X(k)-X(i)); //取三个点维护单调性 } int main() { scanf("%d%d%d",&n,&m,&p); if (p>=m) { puts("0\n"); return 0; } 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); a[i]=(long long)y-d[x]; } sort(a+1,a+1+m); for(int i=1;i<=m;i++) s[i]=s[i-1]+a[i]; for(int i=1;i<=m;i++) dp[0][i]=(long long)1e18; for(i=1;i<=p;i++) { h=t=0; q[0]=0; for(j=0;j<=m;j++) { while (h<t && judge(q[h],q[h+1],j)) h++; //如果h+1比h更优,则继续往后找更优的 dp[i][j]=getdp(q[h]); while (h<t && maintain(q[t],q[t-1],j)) t--; //维护单调性 q[++t]=j; } } printf("%I64d\n",dp[p][m]); return 0; }
相关文章推荐
- CSU-1963 Feed the rabbit(斜率优化dp)
- 文章标题 CSU 1963: Feed the rabbit (斜率DP优化)
- CSU 1963 Feed the rabbit(斜率优化dp)
- 【斜率优化dp】HDU3669 Cross the Wall
- HDU 3669 Cross the Wall (斜率优化DP)
- CodeForces - 311B:Cats Transport (DP+斜率优化)
- CSU1963: Feed the rabbit
- 【codeforces】gym 101138 K. The World of Trains【前缀和优化dp】
- CF 319C - Kalila and Dimna in the Logging Industry 斜率优化DP
- Codeforces 643C Levels and Regions(斜率优化dp)
- Codeforces 320E Kalila and Dimna in the Logging Industry【思维+贪心+斜率优化Dp】好题!
- DP训练 Codeforces 673E Levels and Regions [斜率优化dp][期望]
- CodeForces - 660F:Bear and Bowling 4(DP+斜率优化)
- CodeForces 631E Product Sum(斜率优化DP+二分|三分) ★
- hdu 3669 Cross the Wall(斜率优化DP)
- codeforces 788C The Great Mixing (bitset优化dp、bfs)
- 动态规划DP的斜率优化 个人浅解 附HDU 3669 Cross the Wall
- Codeforces 505C Mr. Kitayuta, the Treasure Hunter DP+技巧优化
- CodeForces 631E Product Sum(斜率优化DP+二分|三分)
- 【bzoj3437】小P的牧场 斜率优化dp