题目:点击打开链接
思路:
令f[i]表示第i天的最大获利,fx[i]表示在第i天最多拥有几张A券,fy[i]表示对应的B券,那么
f[i]=max{fx[j]*a[i]+fy[j]*b[i]}。另外,有fy[i]=f[i]/(a[i]*rate[i]+b[i]) fx[i]=fy[i]*rate[i]。
令fx[j]<fx[k]且k比j更优的条件为:
fx[j]*a[i]+fy[j]*b[i]<fx[k]*a[i]+fy[k]*b[i],移项化简得:(fy[k]-fy[j])/(fx[k]-fx[j])>-a[i]/b[i]。令左边的为k(i,j),于是我们可以维护一个凸包使k单调递减。由于-a[i]/b[i]不具有单调性因此只能在凸包中进行二分查找。
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<numeric>
using namespace std;
#define PI acos(-1.0)
#define LL long long
#define ULL unsigned long long
#define INF 0x3f3f3f3f
#define mm(a,b) memset(a,b,sizeof(a))
#define PP puts("*********************");
template<class T> T f_abs(T a){ return a > 0 ? a : -a; }
template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
// 0x3f3f3f3f3f3f3f3f
// 0x3f3f3f3f
const double eps=1e-9;
const int maxn=120000;
struct Query{
double a,b,rate,k;
int pos;
}q[maxn],nq[maxn];
struct Point{
double x,y;
bool operator<(const Point &rhs)const{
return x<rhs.x+eps||(f_abs(x-rhs.x)<=eps&&y<=rhs.y+eps);
}
}p[maxn],np[maxn];
int stk[maxn];
double f[maxn];
double getk(int i,int j){
if(i==0) return -INF;
if(j==0) return INF;
if(f_abs(p[i].x-p[j].x)<=eps) return -INF;
return (p[i].y-p[j].y)/(p[i].x-p[j].x);
}
void cdq(int L,int R){
if(L==R){
f[L]=max(f[L-1],f[L]);
p[L].y=f[L]/(q[L].a*q[L].rate+q[L].b);
p[L].x=p[L].y*q[L].rate;
return;
}
int mid=(L+R)/2,sz1=L,sz2=mid+1;
for(int i=L;i<=R;i++)
if(q[i].pos<=mid) nq[sz1++]=q[i];
else nq[sz2++]=q[i];
for(int i=L;i<=R;i++) q[i]=nq[i];
cdq(L,mid);
int top=0;
for(int i=L;i<=mid;i++){//构造上凸壳
while(top>=2&&getk(i,stk[top])+eps>getk(stk[top],stk[top-1]))
top--;
stk[++top]=i;
}
int j=1;
for(int i=R;i>mid;i--){
while(j<top&&q[i].k<getk(stk[j+1],stk[j])+eps)
j++;
f[q[i].pos]=max(f[q[i].pos],p[stk[j]].x*q[i].a+p[stk[j]].y*q[i].b);
}
cdq(mid+1,R);
sz1=L,sz2=mid+1;
for(int i=L;i<=R;i++){
if(sz1<=mid){
if(sz2<=R){
if(p[sz1]<p[sz2])
np[i]=p[sz1++];
else
np[i]=p[sz2++];
}
else
np[i]=p[sz1++];
}
else
np[i]=p[sz2++];
}
for(int i=L;i<=R;i++)
p[i]=np[i];
}
bool cmp(Query x,Query y){
return x.k<y.k;
}
int main(){
// freopen("D:\\input.txt","r",stdin);
// freopen("D:\\output.txt","w",stdout);
int n;
scanf("%d%lf",&n,&f[0]);
for(int i=1;i<=n;i++){
scanf("%lf%lf%lf",&q[i].a,&q[i].b,&q[i].rate);
q[i].k=-q[i].a/q[i].b;
q[i].pos=i;
}
sort(q+1,q+n+1,cmp);
cdq(1,n);
printf("%.3f\n",f[n]);
return 0;
}