//Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
// same ids are connected.
// prefix 的格式要求是 acm-[一级标题编号].[二级标题编号][该代码片段的英文简称]
// description 的格式要求是 "插入 [该代码片段的中文名]"

{
    "货币兑换": {
        "prefix": "acm-5.1CDQ斜率优化",
        "body": [
            "#include<algorithm>",
            "#include<cstring>",
            "#include<cstdio>",
            "#define LL long long",
            "#define LD long double",
            "#define Re register int",
            "using namespace std;",
            "const LL N=1e5+3,inf=1e18;",
            "const LD eps=1e-20;",
            "inline int dcmp(LD a){return a<-eps?-1:(a>eps?1:0);}",
            "int n,h,t,Q[N];LD S,A[N],B[N],Ra[N],f[N],dp[N];",
            "inline void in(Re &x){",
            "    int dp=0;x=0;char c=getchar();",
            "    while(c<'0'||c>'9')dp|=c=='-',c=getchar();",
            "    while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();",
            "    x=dp?-x:x;",
            "}",
            "inline void in(LD &X){",
            "\tX=0;LD Y=1.0;int dp=0;char c=getchar();",
            "\twhile(c<'0'||c>'9')dp|=c=='-',c=getchar(); ",
            "\twhile(c>='0'&&c<='9')X=X*10+(c^48),c=getchar(); ",
            "\tif(c=='.'){c=getchar();while(c>='0'&&c<='9')X+=(Y/=10)*(c^48),c=getchar();}",
            "\tX=dp?-X:X;",
            "}",
            "//dp[i]=max((B[i]/A[i])*f[j]+Ra[j]*f[j]),f[i]=dp[i]/(A[i]*Ra[i]+B[i])",
            "//(B[i]/A[i])*f[j]+(-dp[i])=(-Ra[j]*f[j])",
            "//     k     * x  +   b    =     y",
            "#define X(i) (a[i].x)",
            "#define Y(i) (a[i].y)",
            "struct QAQ{",
            "\tLD k,x,y;int id;",
            "\tinline bool operator<(const QAQ &O)const{return dcmp(x-O.x)?x<O.x:y<O.y;}",
            "}a[N],b[N];",
            "inline bool cmp(QAQ A,QAQ B){return dcmp(A.k-B.k)<0;}",
            "inline LD slope(Re i,Re j){return X(i)==X(j)?(Y(j)>Y(i)?inf:-inf):(Y(j)-Y(i))/(X(j)-X(i));}",
            "inline void CDQ(Re L,Re R){",
            "\tif(L==R){",
            "\t\tRe j=a[L].id;",
            "//\t\tprintf(\"getdp(%d), dp=%.3Lf\\n\",j,dp[j]);",
            "\t\tif(j>1)dp[j]=max(dp[j-1],A[j]*dp[j]),f[j]=dp[j]/(A[j]*Ra[j]+B[j]);",
            "\t\ta[L].x=f[j],a[L].y=-Ra[j]*f[j];",
            "//\t\tprintf(\"k=%.3Lf, x=%.3Lf, y=%.3Lf\\n\",a[L].k,a[L].x,a[L].y);",
            "\t\treturn;",
            "\t}",
            "\tRe mid=(L+R)>>1,p1=L,p2=mid+1,h=1,t=0;",
            "\tfor(Re i=L;i<=R;++i)a[i].id<=mid?b[p1++]=a[i]:b[p2++]=a[i];",
            "\tfor(Re i=L;i<=R;++i)a[i]=b[i];",
            "\tCDQ(L,mid);",
            "\tfor(Re i=L;i<=mid;++i){",
            "\t\twhile(h<t&&slope(Q[t-1],Q[t])>=slope(Q[t-1],i))--t;",
            "\t\tQ[++t]=i;",
            "\t}",
            "\tfor(Re i=mid+1,j,id;i<=R;++i){",
            "\t\twhile(h<t&&slope(Q[h],Q[h+1])<=a[i].k)++h;",
            "\t\tif(h<=t)id=a[i].id,j=Q[h],dp[id]=max(dp[id],-(a[j].y-a[i].k*a[j].x));",
            "\t}",
            "\tCDQ(mid+1,R);Re p=L-1;p1=L,p2=mid+1;",
            "\twhile(p1<=mid&&p2<=R)b[++p]=a[p1]<a[p2]?a[p1++]:a[p2++];",
            "\twhile(p1<=mid)b[++p]=a[p1++];while(p2<=R)b[++p]=a[p2++];",
            "\tfor(Re i=L;i<=R;++i)a[i]=b[i];",
            "}",
            "int main(){",
            "//\tfreopen(\"123.txt\",\"r\",stdin);",
            "\tin(n),in(S);",
            "\tfor(Re i=1;i<=n;++i)in(A[i]),in(B[i]),in(Ra[i]),dp[i]=-inf;",
            "\tfor(Re i=1;i<=n;++i)a[i].k=B[i]/A[i],a[i].id=i;",
            "\tf[1]=(dp[1]=S)/(A[1]*Ra[1]+B[1]);",
            "//\tfor(Re i=2;i<=n;++i){",
            "//\t\tfor(Re j=1;j<i;++j)dp[i]=max(dp[i],f[j]*Ra[j]+f[j]*B[i]/A[i]);",
            "//\t\tdp[i]=max(dp[i-1],A[i]*dp[i]),f[i]=dp[i]/(A[i]*Ra[i]+B[i]);",
            "//\t}",
            "\tsort(a+1,a+n+1,cmp),CDQ(1,n);",
            "\tprintf(\"%.3Lf\",dp[n]);",
            "}",
            ""
        ],
        "description": "插入 货币兑换"
    }
}