看网上都是cdq哎,就我是暴力动态维护凸包吗?用vector代码量也还好啊。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#define N 100005
#define eps 1e-8
using namespace std;
int n,m;
vector<int> q;
double f[N],a[N],b[N],rate[N],slop[N];
double calc(int p,int j){
return (slop[j]*rate[j]-slop[p]*rate[p])/(slop[p]-slop[j]);
}
int query(int k){
int l=0,r=q.size()-2,mid,ans=0;
double w=1.0*b[k]/a[k];
while(l<=r){
mid=l+r>>1;
int j=q[mid],p=q[mid+1];
if(w>calc(p,j)) ans=mid+1,l=mid+1;
else r=mid-1;
}
return q[ans];
}
void insert(int k){
int l=0,r=q.size()-1,mid,pos=-1;
while(l<=r){
mid=l+r>>1;
if(slop[q[mid]]<slop[k]+eps) pos=mid,l=mid+1;
else r=mid-1;
}
if(pos>=0&&fabs(slop[q[pos]]-slop[k])<eps){
if(rate[k]<rate[q[pos]]) return;
q.erase(q.begin()+pos);pos--;
}
int L=pos,R=pos+1;
while(L>0&&calc(q[L],q[L-1])>calc(k,q[L])){
L--;
}
r=q.size()-1;
while(R<r&&calc(q[R],k)>calc(q[R+1],q[R])){
R++;
}
q.insert(q.begin()+L+1,k);
for(int i=R;i>L+1;i--) q.erase(q.begin()+i);
L++;
if(L&&L<q.size()-1&&calc(q[L],q[L-1])>calc(q[L+1],q[L]))
q.erase(q.begin()+L);
}
int main(){
scanf("%d%d",&n,&m);
f[1]=m;
for(int i=1;i<=n;i++) scanf("%lf%lf%lf",&a[i],&b[i],&rate[i]);
q.push_back(1);
slop[1]=f[1]/(a[1]*rate[1]+b[1]);
for(int i=2;i<=n;i++){
int pos=query(i);
f[i]=max(slop[pos]*(rate[pos]*a[i]+b[i]),f[i-1]);
slop[i]=f[i]/(a[i]*rate[i]+b[i]);
insert(i);
}
printf("%.3lf",f[n]);
return 0;
}