标签:
10 5000 1 23 45 67 101 124 560 789 990 1019 0 0
30726
斜率优化。
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<functional> #include<iostream> #include<cmath> #include<cctype> #include<ctime> using namespace std; #define For(i,n) for(int i=1;i<=n;i++) #define Fork(i,k,n) for(int i=k;i<=n;i++) #define Rep(i,n) for(int i=0;i<n;i++) #define ForD(i,n) for(int i=n;i;i--) #define RepD(i,n) for(int i=n;i>=0;i--) #define Forp(x) for(int p=pre[x];p;p=next[p]) #define Lson (x<<1) #define Rson ((x<<1)+1) #define MEM(a) memset(a,0,sizeof(a)); #define MEMI(a) memset(a,127,sizeof(a)); #define MEMi(a) memset(a,128,sizeof(a)); #define INF (2139062143) #define F (100000007) #define MAXN (1000000+10) #define eps 1e-13 #define Read(x) { while (!isdigit(c=getchar())); x=c-48; while (isdigit(c=getchar())) x=x*10+c-48; } long long mul(long long a,long long b){return (a*b)%F;} long long add(long long a,long long b){return (a+b)%F;} long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;} typedef long long ll; int n; ll cost; char c; ll a[MAXN],f[MAXN]; struct P { int i; long double x,y; P(int _i,ll _x,ll _y):i(_i),x(_x),y(_y){} P(ll _x,ll _y):x(_x),y(_y){} P(){} friend long double kk(P a,P b){if (abs(a.x-b.x)<eps) return (b.y-a.y)*INF;return (b.y-a.y)/(b.x-a.x); } }st[MAXN]; struct V { long double x,y; V(ll _x,ll _y):x(_x),y(_y){} V(){} V(P a,P b):x(b.x-a.x),y(b.y-a.y){} friend long double operator*(V a,V b){return a.x*b.y-a.y*b.x; } }; int main() { freopen("B.in","r",stdin); while (1) { scanf("%d%lld",&n,&cost); if (!n) break; For(i,n) Read(a[i]) f[0]=0; int head=1,tail=1; st[1]=P(1,a[1],a[1]*a[1]); Fork(i,1,n) { P A=P(i,a[i],f[i-1]+a[i]*a[i]); while (head^tail&&V(st[tail-1],st[tail])*V(st[tail],A)<=0) tail--; st[++tail]=A; while (head^tail&&kk(st[head],st[head+1])<2*a[i]) head++; int k=st[head].i; f[i]=f[k-1]+cost+(a[k]-a[i])*(a[k]-a[i]); } cout<<f[n]<<endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 4258(Covered Walkway-斜率优化)
标签:
原文地址:http://blog.csdn.net/nike0good/article/details/47610799