bzoj 1010: [HNOI2008]玩具装箱toy
这自己推的式子长的要死,最后还把j^2+k^2掉去了,,,
%%%http://hzwer.com/2114.html
1 /*#include <bits/stdc++.h> 2 #define LL long long 3 #define lowbit(x) x&(-x) 4 #define inf 0x3f3f3f3f 5 #define eps 1e-5 6 #define N 50005 7 using namespace std; 8 inline LL ra() 9 { 10 LL x=0,f=1; char ch=getchar(); 11 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 12 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 13 return x*f; 14 } 15 LL L,sum[N],f[N]; 16 LL n,q[N]; 17 double slope(LL j, LL k) 18 { 19 return (f[j]+(sum[j]+L)*(sum[j]+L)-f[k]-(sum[k]+L)*(sum[k]+L))/(double)(sum[j]-sum[k]); 20 } 21 int main() 22 { 23 n=ra(); L=ra(); L++; 24 for (LL i=1; i<=n; i++) sum[i]=sum[i-1]+(LL)ra(); 25 for (int i=1; i<=n; i++) sum[i]+=i; 26 LL l=0,r=0; 27 for (LL i=1; i<=n; i++) 28 { 29 while (l<r && slope(q[l+1],q[l])<=2*sum[i]) l++; 30 LL t=q[l]; 31 LL X=(sum[i]-sum[t]); 32 f[i]=f[t]+(X-L)*(X-L); 33 while (l<r && slope(i,q[r])<=slope(q[r],q[r-1])) r--; 34 q[++r]=i; 35 } 36 cout<<f[n]; 37 return 0; 38 }*/ 39 #include <bits/stdc++.h> 40 #define LL long long 41 #define lowbit(x) x&(-x) 42 #define inf 0x3f3f3f3f 43 #define eps 1e-5 44 #define N 50005 45 using namespace std; 46 inline LL ra() 47 { 48 LL x=0,f=1; char ch=getchar(); 49 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 50 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 51 return x*f; 52 } 53 LL L,sum[N],f[N]; 54 LL n,q[N]; 55 double slope(LL j, LL k) 56 { 57 return (double)(f[j]+sum[j]*sum[j]+2*sum[j]*j+j*j-f[k]-sum[k]*sum[k]-2*sum[k]*k-k*k)/(double)(2.0*(sum[j]-sum[k]+j-k)); 58 } 59 int main() 60 { 61 n=ra(); L=ra(); 62 for (LL i=1; i<=n; i++) sum[i]=sum[i-1]+(LL)ra(); 63 LL l=0,r=0; 64 for (LL i=1; i<=n; i++) 65 { 66 while (l<r && slope(q[l+1],q[l])<=(sum[i]+i-L-1)) l++; 67 LL t=q[l]; 68 LL X=(sum[i]-sum[t]+i-t-1); 69 f[i]=f[t]+(X-L)*(X-L); 70 while (l<r && slope(i,q[r])<=slope(q[r],q[r-1])) r--; 71 q[++r]=i; 72 } 73 cout<<f[n]; 74 return 0; 75 }