Topic Connection: Hdu 3669 cross the Wall
Test instructions
Now there is an infinite wall, now there are n people, everyone can be seen as a rectangle, width is W, High is H, now this n person to pass this wall, now only let you dig k hole, each hole can not overlap, each hole need to consume you dig w*h, now ask you how much minimum consumption.
Exercises
Set DP[I][J] for the first J personal digging I hole minimum consumption.
First we sort each person by the W, and we can see that the ordered H is decremented, because if it is not descending, it can be eliminated, because W[k]<w[j]&&h[k]
Then we can preprocess it.
Then you can have
Dp[i][j]=min{dp[i-1][k]+w[j]*h[k+1]} (K<J)
Then consider the slope optimization
Set k>l, for Dp[i][j], the K-person to the J-person through a hole than the first person to the J-man through a hole better
Have dp[i-1][k]+w[j]*h[k+1]<=dp[i-1][l]+w[j]*h[l+1];
Organized to
dp[i-1][k]-dp[i-1][l]/h[l+1]-h[k+1]<=w[j];
And then the monotone queue optimizes it just fine.
1#include <bits/stdc++.h>2 #defineF (I,A,B) for (int i=a;i<=b;++i)3 using namespacestd;4typedefLong Longll;5 6 Const intn=5e4+7;7 intn,k,q[n],ed;8ll dp[101][n],ans,inf=1ll<< -;9 structnodeTen { One ll W,h; A BOOL operator< (ConstNode &b)Const{returnw<B.W;} - }a[n],b[n]; - thell Getx (intKintL) {return-b[k+1].h+b[l+1].h;} -LL Gety (intIintKintL) {returndp[i][k]-dp[i][l];} -LL Check (intIintJintKintL) {returnGety (i,j,k) *getx (k,l) <=gety (i,k,l) *Getx (j,k);} - + intMain () - { + while(~SCANF ("%d%d",&n,&k)) A { atF (I,1, N) scanf ("%lld%lld",&a[i].w,&a[i].h); -Sort (A +1, A +1+n), ed=0; -F (I,1, N) - { - while(ed&&a[i].h>b[ed].h) ed--; -b[++ed]=A[i]; in } -F (I,1, ed) dp[1][i]=b[i].w*b[1].h; toF (I,2, K) + { - intHead=1, tail=0; theq[++tail]=i-1; * F (j,i,ed) $ {Panax Notoginseng while(Head<tail&&gety (i1, q[head+1],q[head]) <=b[j].w*getx (q[head+1],q[head]) head++; -dp[i][j]=dp[i-1][q[head]]+b[j].w*b[q[head]+1].h; the while(Head<tail&&check (i1, j,q[tail],q[tail-1])) tail--; +q[++tail]=J; A } the } +ans=inf; -F (I,1Kif(Ans>dp[i][ed]) ans=dp[i][ed]; $printf"%lld\n", ans); $ } - return 0; -}
View Code
Hdu 3669 cross the Wall (slope optimized DP)