bzoj1010
1.HDU3507
2.bzoj1010
根据动规方程状态i从状态j转化而来,
y只与j有关,k与i,j有关,b只与i或常数有关,可得直线方程y=kx+b,本题让b尽量小,故维护下凸包,使k单调递增
则已确认了当前直线的斜率,取得的j即为当前直线向左碰到的第一个点
无论如何,一定不会碰到斜率更小的,更新head;插入当前点,同时维护下凸包性质,更新tail
#include<cstdio> #include<algorithm> using namespace std; #define maxn 50002 long long s[maxn],q[maxn],f[maxn]; double Y(long long j){return f[j]+s[j]*s[j];} double X(long long j){return s[j];} double slop(long long j,long long k){return(Y(k)-Y(j))/(X(k)-X(j));} int main(){ long long n,L,i,x,head,tail; scanf("%lld%lld",&n,&L);L++;s[0]=0; for(i=1;i<=n;i++){scanf("%lld",&s[i]);s[i]+=s[i-1];} for(i=1;i<=n;i++)s[i]+=i; head=tail=1;q[1]=0; for(i=1;i<=n;i++){ while(head<tail && slop(q[head],q[head+1])<=2*(s[i]-L))head++; int j=q[head]; f[i]=f[j]+(s[i]-s[j]-L)*(s[i]-s[j]-L); while(head<tail && slop(q[tail-1],q[tail])>slop(q[tail],i))tail--; q[++tail]=i; } printf("%lld\n",f[n]); return 0; }