【BZOJ】【P1492】【NOI2007】【货币兑换Cash】【题解】【cdq分治】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1492

f[i]表示第i天所能获得的最大RMB

Ai 第i天A券数目
Bi 第i天B券数目
ai 第i天A券价值
bi 第i天B券价值
ratei 第i天比率

显然


其中


这是一个典型的斜率优化……

原本需要Splay维护凸壳

考虑分治

定义solve(l,r)表示计算出[l,r]的f[i]和凸包

那么

solve(l,r):

solve(l,mid)

更新[mid+1,r]的f[i]

solve(mid+1,r)

个人感觉我的代码写的还是不错的……

Code:

#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
const double eps=1e-7;
double rate[maxn],f[maxn],A[maxn],B[maxn],a[maxn],b[maxn];
int n;double S;
int dcmp(double x){return x<-eps?-1:x>eps;}
struct point{double x,y;point(double _x=0,double _y=0):x(_x),y(_y){}};
double operator*(point a,point b){return a.x*b.y-a.y*b.x;}
double operator^(point a,point b){return a.x*b.x+a.y*b.y;}
double operator<(point a,point b){return dcmp(a.x-b.x)<0||(dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)<0);}
point operator+(point a,point b){return point(a.x+b.x,a.y+b.y);}
point operator-(point a,point b){return point(a.x-b.x,a.y-b.y);}
struct CoverHull{
	vector<point>ch;
	void push_back(point p){
		while(ch.size()>1&&dcmp((p-ch.back())*(ch.back()-ch[ch.size()-2]))<=0)ch.pop_back();
		ch.push_back(p);
	}
	double Qmax(point p){
		double ans=-1e30;
		int l=0,r=ch.size()-1;
		while(r-l>2){
			int mid1=l+(r-l)/3;
			int mid2=r-(r-l)/3;
			if((p^ch[mid1])>(p^ch[mid2]))
				r=mid2;
			else l=mid1;			
		}for(int i=l;i<=r;i++)ans=max(ans,p^ch[i]);
		return ans;
	}
};
CoverHull operator+(CoverHull A,CoverHull B){
	CoverHull C;
	vector<point>ch(A.ch.size()+B.ch.size());
	merge(A.ch.begin(),A.ch.end(),B.ch.begin(),B.ch.end(),ch.begin());
	for(int i=0;i<ch.size();i++)C.push_back(ch[i]);
	return C;
}
CoverHull solve(int l,int r){
	if(l==r){
		f[l]=max(f[l],f[l-1]);
		B[l]=f[l]/(rate[l]*a[l]+b[l]);
		A[l]=B[l]*rate[l];		
		CoverHull M;M.push_back(point(A[l],B[l]));
		return M;
	}int mid=(l+r)>>1;
	CoverHull L,R;
	if(l<=mid)L=solve(l,mid);
	for(int i=mid+1;i<=r;i++){
		f[i]=max(f[i],f[i-1]);
		f[i]=max(f[i],L.Qmax(point(a[i],b[i])));		
	}if(mid+1<=r)R=solve(mid+1,r);		
	return L+R;
}
int main(){
	scanf("%d%lf",&n,&S);
	f[0]=S;
	for(int i=1;i<=n;i++)scanf("%lf%lf%lf",&a[i],&b[i],&rate[i]);
	solve(1,n);
	printf("%.3lf\n",f[n]);
	return 0;
}


你可能感兴趣的:(bzoj)