[NOI2007]货币兑换Cash(DP+动态凸包)

时间:2023-03-08 20:06:52
[NOI2007]货币兑换Cash(DP+动态凸包)

第一次打动态凸包维护dp,感觉学到了超级多的东西。

首先,set是如此的好用!!!可以通过控制一个flag来实现两种查询,维护凸包和查找斜率k

不过就是重载运算符和一些细节方面有些恶心,90行解决

后面还有一个cdq分治,找时间学下,看下能不能处理一大类恶心的问题

github还是不会用,找时间搞下吧

CODE:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
using namespace std;
const double esp=1e-;
const int maxn=;
int dcmp(double a) {
if (fabs(a)<esp) return ;
return a<?-:;
}
struct node{
double x,y,k;bool f;
node(double _x,double _y):x(_x),y(_y),f(){}
node():f(){}
inline bool error(){return x>1e50;}
}error(1e51,);
inline bool operator == (const node x,const node y) {return dcmp(x.x-y.x)==;}
inline bool operator < (const node x,const node y){
if (x.f||y.f) return dcmp(x.k-y.k)>;
else return dcmp(x.x-y.x)<;
}
inline double operator *(const node x,const node y) {return x.x*y.y-x.y*y.x;}
inline node operator -(const node x,const node y){
node tmp;
tmp.x=x.x-y.x;tmp.y=x.y-y.y;
return tmp;
}
inline double calc_k(node x,node y) {return (x.y-y.y)/(x.x-y.x);}
set<node> gap;
typedef set<node>::iterator iter;
inline node lower(node x) {
iter it=gap.lower_bound(x);
return it==gap.end()?error:*it;
}
inline node next(node x) {
iter it=gap.upper_bound(x);
return it==gap.end()?error:*it;
}
inline node pre(node x) {
iter it=gap.lower_bound(x);
return it==gap.begin()?error:*(--it);
}
inline void ins(node a) {
if (gap.empty()) {a.k=;gap.insert(a);return;}
node d1=pre(a),d2=lower(a);
if (d1.error()&&d2.y>a.y) return ;
if ((!d1.error())&&(!d2.error())&&(dcmp((d2-d1)*(a-d1))<=)) return ;
if (d2==a) return;
node p1,p2=next(a);
for (;;){
p1=p2;p2=next(p2);
if (p1.error()||p2.error()) break;
if (dcmp((p1-a)*(p2-a))<=) break;
gap.erase(p1);
}
p2=pre(a);
for (;;){
p1=p2;p2=pre(p2);
if (p1.error()||p2.error()) break;
if (dcmp((p1-a)*(p2-a))>=) break;
gap.erase(p1);
}
d1=pre(a),d2=next(a);
if (d1.error()) a.k=;else a.k=calc_k(d1,a);gap.insert(a);
if (!d2.error()) gap.erase(d2),d2.k=calc_k(a,d2),gap.insert(d2);
}
inline double get_k(double a,double b) {
node tmp;tmp.f=;tmp.k=-a/b;
tmp=*(--gap.lower_bound(tmp));
return a*tmp.x+b*tmp.y;
}
double a[maxn],b[maxn],r[maxn],na[maxn],nb[maxn],f[maxn];
int main(){
int n,s;
scanf("%d%d",&n,&s);
for (int i=;i<=n;i++) scanf("%lf%lf%lf",a+i,b+i,r+i);
f[]=s;
nb[]=f[]/(a[]*r[]+b[]);
na[]=nb[]*r[];
ins(node(na[],nb[]));
for (int i=;i<=n;i++) {
f[i]=max(f[i-],get_k(a[i],b[i]));
nb[i]=f[i]/(a[i]*r[i]+b[i]);
na[i]=nb[i]*r[i];
ins(node(na[i],nb[i]));
}
printf("%.3f\n",f[n]);
}