差不多就是维护两个set,一个是按照横坐标建的,一个是按照斜率建的,顺便记一下对应哪个点
本机测A了,但是bz上0s RE,求解释?
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<iostream> #include<set> #define maxn 100100 #define inf 1000000000 #define pa pair<double,int> #define eps 1e-8 using namespace std; struct yts { double x,y; int id; }P[maxn]; bool operator<(yts x,yts y) { return x.x<y.x || (x.x==y.x && x.y<y.y); } yts operator-(yts x,yts y) { yts ans; ans.x=x.x-y.x;ans.y=x.y-y.y; return ans; } double operator*(yts x,yts y) { return x.x*y.y-x.y*y.x; } set<yts> p; set<pa> q; double f[maxn],a[maxn],b[maxn],rate[maxn]; int n,S; double get_slope(yts x,yts y) { if (abs(y.x-x.x)<eps) return inf; return (y.y-x.y)/(y.x-x.x); } void print(yts x) { printf("%.3lf %.3lf %d\n",x.x,x.y,x.id); } void insert(yts P) { set<yts>::iterator r=p.lower_bound(P),l=r,t; l--; if (r==p.end()) { set<pa>::iterator R=q.begin(); while (l!=p.begin()) { t=l;l--; if ((*l-P)*(*t-P)<0) break; q.erase(R++);p.erase(t); } p.insert(P); l=r=t=p.find(P); l--; q.insert(make_pair(get_slope(*l,P),P.id)); } else { set<pa>::iterator L=q.find(make_pair(get_slope(*l,*r),(*r).id)),R=L; R++; if ((P-*l)*(*r-P)>=0) return; q.erase(L--); while (1) { t=r;r++; if (r==p.end()) break; if ((*t-P)*(*r-P)<0) break; q.erase(L--);p.erase(t); } while (l!=p.begin()) { t=l;l--; if ((*l-P)*(*t-P)<0) break; q.erase(R++);p.erase(t); } p.insert(P); l=r=t=p.find(P); r++;l--; q.insert(make_pair(get_slope(*l,P),P.id)); q.insert(make_pair(get_slope(P,*r),(*r).id)); } } double query(int i) { double slope=0.0; if (abs(b[i])<eps) slope=inf; else slope=-a[i]/b[i]; set<pa>::iterator Q=q.lower_bound(make_pair(slope,-inf)); int now; if (Q==q.end()) now=0; else now=(*Q).second; if (now==0) now++; return P[now].x*a[i]+P[now].y*b[i]; } int main() { scanf("%d%d",&n,&S); yts Q; Q.x=0;Q.y=0;Q.id=0;p.insert(Q); for (int i=1;i<=n;i++) { scanf("%lf%lf%lf",&a[i],&b[i],&rate[i]); if (i==1) f[i]=S; else f[i]=max(query(i),f[i-1]); P[i].x=f[i]/(a[i]*rate[i]+b[i])*rate[i]; P[i].y=f[i]/(a[i]*rate[i]+b[i]); P[i].id=i; insert(P[i]); } printf("%.3lf\n",f[n]); return 0; }