标签:targe 方便 hellip 维护 size logs data- stack cstring
Description
Solution
论文题啦…
CDQ分治看起来比平衡树维护凸包要友好的多…
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #define MAXN 100005 #define eps 1e-9 const double INF=1e100; using namespace std; int n,s,stack[MAXN],top; double a[MAXN],b[MAXN],R[MAXN],f[MAXN]; struct Node { double s,x,y; int id; bool operator < (const Node& x) const {return s<x.s;} }data[MAXN],t[MAXN]; double slope(int i,int j) { if(fabs(data[j].x-data[i].x)<eps)return data[j].y>data[i].y?INF:-INF; return (data[j].y-data[i].y)/(data[j].x-data[i].x); } void merge(int l,int r) { if(l==r)return; int mid=(l+r)>>1; int i=l,j=mid+1,k=l; while(i<=mid&&j<=r) { if(data[i].x<data[j].x)t[k++]=data[i++]; else t[k++]=data[j++]; } while(i<=mid)t[k++]=data[i++]; while(j<=r)t[k++]=data[j++]; for(int i=l;i<=r;i++)data[i]=t[i]; } void solve(int l,int r) { if(l==r) { f[l]=max(f[l],f[l-1]); data[l].y=f[l]/(R[l]*a[l]+b[l]); data[l].x=data[l].y*R[l]; return; } int mid=(l+r)>>1;for(int i=l,j=mid+1,k=l;k<=r;k++) if(data[k].id<=mid)t[i++]=data[k]; else t[j++]=data[k]; for(int i=l;i<=r;i++)data[i]=t[i]; solve(l,mid); top=0; for(int i=l;i<=mid;i++) { while(top>1&&slope(stack[top-1],stack[top])<eps+slope(stack[top],i)) top--; stack[++top]=i; } for(int i=mid+1;i<=r;i++) { while(top>1&&data[i].s+eps>slope(stack[top-1],stack[top])) top--; f[data[i].id]=max(f[data[i].id],data[stack[top]].x*a[data[i].id]+data[stack[top]].y*b[data[i].id]); } solve(mid+1,r); merge(l,r); } int main() { scanf("%d%lf",&n,&f[0]); for(int i=1;i<=n;i++) { scanf("%lf%lf%lf",&a[i],&b[i],&R[i]); data[i].id=i,data[i].s=-a[i]/b[i]; } sort(data+1,data+1+n); solve(1,n); printf("%.3lf\n",f[n]); return 0; }
[BZOJ 1492][NOI2007]货币兑换Cash(CDQ分治+斜率优化Dp)
标签:targe 方便 hellip 维护 size logs data- stack cstring
原文地址:http://www.cnblogs.com/Zars19/p/6892972.html