第一行两个数 n、m。
第二行 n 个数,表示 n 段路的长度
标签:har ble define col data 开始 休息 2.0 bsp
一个数,最小方差乘以 m^2 后的值
1≤n≤3000,保证从 S 到 T 的总路程不超过 30000
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; int I(){int x=0,f=1;char c=getchar(); for(;c<‘0‘||c>‘9‘;c=getchar())if(c==‘-‘)f=-1; for(;‘0‘<=c&&c<=‘9‘;c=getchar())x=(x<<3)+(x<<1)+(c^48);return x*f;} #define M 3010 ll f[M],s[M],tmp[M],q[M],l,r,a[M]; #define g(x) (tmp[x] + s[x]*s[x]) inline double T(ll p,ll q){return (double)(g(p)-g(q))/(double)(s[p]-s[q]);} int main(){ll n,k;n=I();k=I();ll sum=0; for(ll i=1;i<=n;++i)a[i]=I(),sum+=a[i],s[i]=s[i-1]+a[i],tmp[i]=s[i]*s[i]; for(ll j=2;j<=k;++j){l=0;r=-1;q[++r]=j-1; for(ll i=j;i<=n;++i){ while(l<r&&T(q[l],q[l+1])<2.0*s[i])++l; f[i]=tmp[q[l]]+(s[i]-s[q[l]])*(s[i]-s[q[l]]); while(l<r&&T(q[r-1],q[r])>T(q[r],i))--r;q[++r]=i; }memcpy(tmp,f,sizeof f);}printf("%lld\n",f[n]*k-sum*sum); return 0;}
标签:har ble define col data 开始 休息 2.0 bsp
原文地址:http://www.cnblogs.com/muzu/p/7966482.html