标签:des c style class blog code
Time
Limit: 9000/3000 MS (Java/Others) Memory
Limit: 131072/65536 K (Java/Others)
Total Submission(s):
4810 Accepted Submission(s):
1451
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; #define MAXN 510000 #define INF 0x3f3f3f3f //AC int n,m; int num[MAXN]; typedef long long qword; qword f[MAXN]; qword sum[MAXN]; inline qword sqr(int x) { return x*x; } //f[i]=min(f[j]-2*sum[j]*sum[i]+sum[j]^2)+sum[i]^2+m //f[i]=-2*sum[j]*sum[i] + f[j]+sum[j]^2 +sum[i]^2 + m // -2*sum[i]*sum[j] + sum[i]^2-f[i]+m == -f[j]-sum[j]^2 struct Point { qword x,y; void init(qword xx,qword yy) { x=xx;y=yy; } }; Point make_point (qword x,qword y) { Point ret; ret.init(x,y); return ret; } qword xmul(Point p1,Point p2,Point p3) { return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x); } double get_k(Point p1,Point p2) { if (p1.x==p2.x)throw "error"; return (double)(p2.y-p1.y)/(p2.x-p1.x); } struct Convex_Hall { Point pl[MAXN]; double kk[MAXN]; int topl; void clear() { topl=-1; } Convex_Hall() { topl=-1; } void add_point(Point pp) { if (topl<1) { pl[++topl]=pp; if (topl==1&&pl[topl].x==pl[topl-1].x) { if (pl[topl].y<pl[topl-1].y) { topl--; return ; }else { pl[topl--]=pp; return ; } } if (topl==1) { kk[topl-1]=get_k(pl[topl-1],pl[topl]); } return ; } while (topl>=1&&xmul(pl[topl-1],pl[topl],pp)>=0) { topl--; } pl[++topl]=pp; kk[topl-1]=get_k(pl[topl-1],pp); } void pm() { int i; for(i=0;i<=topl;i++) { printf("(%d,%d) ",pl[i].x,pl[i].y); } printf("\n"); } double get_maxb(double k) { int l,r,mid; if (topl==-1)throw "Error"; if (topl==0)return pl[0].y-pl[0].x*k; if (k>kk[0])return pl[0].y-pl[0].x*k; if (k<kk[topl-1])return pl[topl].y-pl[topl].x*k; l=0,r=topl; while (l<r) { mid=(l+r)>>1; if (kk[mid-1]>=k&&kk[mid]<=k) { return pl[mid].y-pl[mid].x*k; } if (kk[mid-1]<k) { r=mid; }else { l=mid; } } } }H; int main() { //freopen("input.txt","r",stdin); int i; while (~scanf("%d%d",&n,&m)) { H.clear(); for (i=1;i<=n;i++) { scanf("%d",&num[i]); sum[i]=sum[i-1]+num[i]; } memset(f,INF,sizeof(f)); /* f[0]=0; for (i=1;i<=n;i++) { for (j=0;j<i;j++) { if (f[j]>=INF)continue; f[i]=min(f[i],f[j]+sqr(sum[i]-sum[j])+m); } } for (i=1;i<=n;i++)cout<<f[i]<<" ";cout<<endl; */ f[0]=0; H.add_point(make_point(sum[0],-sum[0]*sum[0]-f[0])); double k,b; for (i=1;i<=n;i++) { k=-2*sum[i]; b=H.get_maxb(k); f[i]=sum[i]*sum[i]+m-ceil(b); H.add_point(make_point(sum[i],-sum[i]*sum[i]-f[i])); // cout<<f[i]<<" "; } cout<<f[n]<<endl;; } return 0; }
HDU 3507 Print Article 斜率优化,布布扣,bubuko.com
标签:des c style class blog code
原文地址:http://www.cnblogs.com/mhy12345/p/3769028.html