Bzoj [NOI2007] Currency exchange Cash (CDQ Division + slope Optimization)

Source: Internet
Author: User
Tags cmath

1492: [NOI2007] Currency exchange cash time limit: 5 Sec Memory Limit: MB
Submit: 2454 Solved: 1078
[Submit] [Status] [Discuss] Descriptioninput the first row of two positive integers n, S, respectively, indicating the number of days the small Y can predict and the initial amount of money. Next n lines, the K line three real AK, BK, Ratek, meaning as described in the title output has only a real maxprofit, indicating the maximum amount of money that can be obtained at the end of the nth day operation. The answer retains 3 decimal places. Sample Input3 100
1 1 1
1 2 2
2 2 3Sample Output
225.000


According to CDQ's thesis, N*LGN should be written. However, I forcibly scan the convex hull every time, the complexity of more than one LGN t_t.
#include <cstdio> #include <cmath> #include <queue> #include <stack> #include <string># include<cstring> #include <iostream> #include <map> #include <vector> #include <algorithm > #include <stdlib.h> #include <set> #include <ctime> #include <cmath> #include <iomanip >using namespace Std;typedef long long ll;const int mmax = 100010;const int inf = 0x3fffffff;const double EPS = 1e-8;d Ouble A[mmax],b[mmax],r[mmax];d ouble F[mmax];d ouble dp[mmax];int sgn (double x)//ûºåº¯êý{if (fabs (x) <eps) re    Turn 0; return x<0?-1:1;}    struct point{double x, y;    Point (Double x=0.0,double y=0.0): X (x), Y (y) {} void Read () {scanf ("%lf%lf", &x,&y); }};typedef Point Vector; Vector operator + (vector a,vector B) {return vector (A.X+B.X,A.Y+B.Y);} Vector operator-(point A,point B) {return Vector (A.X-B.X,A.Y-B.Y);} Vector operator * (vector a,double p) {return vector (a.x*p,a.y*p);} Vector operator/(Vector a,double p) {return Vector (a.x/p,a.y/p);} BOOL operator < (const point &a,const point &b) {return a.x<b.x | | (A.X==B.X&AMP;&AMP;A.Y&LT;B.Y);} ôëë㲿 Ödouble Dot (Vector a,vector B) {return a.x*b.x+a.y*b.y;} Double Cross (Vector a,vector B) {return a.x*b.y-a.y*b.x;} Double x (int i) {return dp[i]*r[i]/f[i];} Double y (int i) {return dp[i]/f[i];} BOOL CMP (int I,int j) {return a[i]*b[j]>a[j]*b[i];}    int Convexhull (point *p,int n,point *poly) {sort (p,p+n);    int m=0;        for (int i=n-1;i>=0;i--) {When (M>1 && Cross (poly[m-1]-poly[m-2],p[i]-poly[m-2)) <=0) m--;    Poly[m++]=p[i]; } return m;}        Point P[mmax],poly[mmax];int tmp[mmax];void CDQ (int l,int r) {if (l==r) {Dp[l]=max (dp[l],dp[l-1]);    return;    } int mid= (L+R) >>1;    CDQ (L,mid);    int cnt=0;    for (int i=l;i<=mid;i++) p[cnt++]=point (x (i), Y (i));    Cnt=convexhull (P,cnt,poly); for (int i=mid+1;i<=r;i++) tmp[i]=i;    Sort (tmp+mid+1,tmp+r+1,cmp);    int i=0,j=mid+1; while (J<=r) {while (i<cnt-1 && (POLY[I].Y-POLY[I+1].Y) *b[tmp[j]] < -1.0* (poly[i].x-poly[i+1].x        ) *a[tmp[j]) i++;        Dp[tmp[j]]=max (DP[TMP[J]],A[TMP[J]]*POLY[I].X+B[TMP[J]]*POLY[I].Y);    j + +; } CDQ (Mid+1,r);}    int main () {int n,s;    cin>>n>>s;        for (int i=1;i<=n;i++) {scanf ("%lf%lf%lf", &a[i],&b[i],&r[i]);    F[i]=a[i]*r[i]+b[i];    } memset (dp,0,sizeof DP);    dp[0]=1.0*s;    CDQ (1,n);    Double ans=0;    for (int i=1;i<=n;i++) Ans=max (Ans,dp[i]);    printf ("%.3lf\n", ans); return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bzoj [NOI2007] Currency exchange Cash (CDQ Division + slope Optimization)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.