斜率优化错误总结
网上说很多OJ桑的斜率优化大多都是模板题- -,结果每次都跪Orz。。。在此总结一些常见错误:
1:不得不说斜率优化很多时候计算式很长- -,代码容易错细节- -。
2:其次就是弹队头以及弹队尾的时候大小关系容易错,关键是除负数时要变号,
弹的时候边界要弄好,总的来说很多斜率优化都很像:推式子——>边弹边做边入队;
3:再者就是一些先后问题:
在处理队列的时候,尤其是弹队尾的时候,如果有推导式中有未知量要先计算,不然会弹WA- -;
4:还有队列初始化问题- -要机智点- -;
代码:
1:锯木厂选址(CEOI2004试题)
1 #include<cstdio> 2 int n,f=1,r,top=1,tai=1,ans=888888888,c[20010],w[20010],d[20010],s[20010]; 3 int all(int a,int b){return c[b]-c[a]-w[a-1]*(d[b]-d[a]); } 4 double k(int a,int b){return (double)(w[a]*d[a]-w[b]*d[b])/(double)(w[a]-w[b]);} 5 int main(){ 6 scanf("%d",&n); s[1]=1; 7 for (int i=1; i<=n ; i++) scanf("%d%d",&w[i],&d[i+1]); 8 for (int i=2; i<=n+1; i++) w[i] += w[i-1]; 9 for (int i=2; i<=n+1; i++) c[i] = c[i-1]+w[i-1]*d[i] ; 10 for (int i=2; i<=n+1; i++) d[i] += d[i-1]; 11 for (int i=2; i<=n; i++){ 12 while (top< tai&&k(s[top],s[top+1])<= d[ i ] ) top++; 13 while (top<=tai&&k(s[tai-1],s[tai])>=k(s[tai],i)) tai--; 14 r = c[ s[top] ] + all(s[top]+1,i) + all(i+1,n+1); 15 if (r<ans) ans=r; s[++tai]=i; 16 } printf("%d",ans); 17 }
2:玩具装箱 (HNOI2008试题)
1 #include<cstdio> 2 long long top=0,tai=0,s[50010],f[50010],n,l,K,h[50010]; 3 long double k(int a,int b) { 4 return (long double)(f[b]+h[b]*h[b]-f[a]-h[a]*h[a]) 5 /(long double)(h[b]-h[a]); 6 } 7 int main(){ 8 scanf("%lld%lld",&n,&l); 9 for (int i=1; i<=n; i++) scanf("%lld",&h[i]); 10 for (int i=1; i<=n; i++) h[i]+=h[i-1]+1; 11 for (int i=1; i<=n; i++) { 12 K=h[i]-1-l; 13 while (top< tai&&k(s[top],s[top+1])<=2*K) top++; 14 f[i]=f[s[top]]+(K-h[s[top]])*(K-h[s[top]]); 15 while (top<=tai&&k(s[tai-1],s[tai])>=k(s[tai],i)) tai--; 16 s[++tai]=i; 17 } printf("%lld",f[n]); 18 }
3:土地并购 (USACO精选49)
1 #include<cstdio> 2 #include<algorithm> 3 struct p{long long a,b;}t[50010]; 4 long long l,n,top=0,tai=0,f[50010],s[50010]; 5 bool cmp(const p&x,const p&y) {return (x.a<y.a)||(x.a==y.a&&x.b<y.b);} 6 long double k(long long j1,long long j2 ) { 7 return (long double)(f[j1]-f[j2])/ 8 (long double)(t[j2+1].b-t[j1+1].b); 9 } 10 int main(){ 11 scanf("%lld",&n); 12 for (int i=1; i<=n; i++) scanf("%lld%lld",&t[i].a,&t[i].b); 13 std::sort(t+1,t+n+1,cmp); 14 for (int i=1; i<=n; i++) { 15 while (l&&t[i].b>=t[l].b) l--; 16 t[++l].a=t[i].a; 17 t[ l].b=t[i].b; 18 } 19 for (int i=1; i<=l; i++) { 20 while (top< tai&&k(s[top],s[top+1])<= t[ i ].a ) top++; 21 f[i]=f[s[top]]+t[i].a*t[s[top]+1].b; 22 while (top<=tai&&k(s[tai-1],s[tai])>=k(s[tai],i)) tai--; 23 s[++tai]=i; 24 } printf("%lld",f[l]); 25 }