"BZOJ-1492" Currency exchange cash DP + slope optimization + CDQ Division

Source: Internet
Author: User

1492: [NOI2007] Currency exchange cash time limit:5 Sec Memory limit:64 MB
submit:3396 solved:1434
[Submit] [Status] [Discuss] Description Small Y recently worked on a Gold exchange. The Gold Exchange only issues two types of gold coupons: A commemorative voucher (hereinafter referred to as a coupon) and B commemorative voucher (hereinafter referred to as B voucher). Each customer who holds a gold coupon has an account of his own. The number of gold coupons can be a real. With the fluctuation of the market every day, both kinds of gold coupons have their own value at that time, that is, the number of RMB that can be exchanged on the day of each unit gold coupon. We recorded the value of a and B coupons in K days respectively as AK and BK (Yuan/Unit gold coupon). For the convenience of customers, the Gold Exchange provides a very convenient way to trade: proportional Trading method. The proportional trading method is divided into two areas: (a) selling a gold coupon: The customer provides a real OP in [0,100] as a sell ratio, meaning: The op% 's a coupon and the B-coupon of op% are converted to renminbi at the current value; (b) Buy Gold coupon: Customers pay IP yuan, the exchange will be redeemed to the user's total price The value is the gold coupon of the IP, and the proportion of the A and B coupons that are provided to the customer happens to be Ratek on the K-day; For example, assume that the change of Ak, Bk, and Ratek in the next 3 days are: Assuming that on the first day, the user has 100 yuan but no gold coupon. The user can do the following: Notice that multiple operations can be performed within the same day. Small y is a very economic-minded staff, through a long period of operation and market estimates, he has been aware of the next n days of the value of a and B coupons and rate. He also wanted to be able to figure out how much it would take for n days to start with S-Dollars. Input enters the first line two positive integers n, S, respectively, indicating the number of days that small y can predict and the amount of money that was initially held. Next n lines, the K line three real numbers AK, BK, Ratek, meaning as described in the topic. For 100% of test data, meet: 0<ak≤10;0<bk≤10;0<ratek≤100;maxprofit≤10^9. "Tip" 1. The input file may be large, please use a fast read-in mode. 2. There must be an optimal buying and selling solution: use all RMB for each purchase operation, and sell all Gold coupons for each sell operation. Output

There is only one real maxprofit, which represents 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 Output225.000HINT

SourceSolution

It's a hole for the time being, it hasn't been transferred.

Code
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespacestd;#defineMAXN 200010structdaynode{DoubleA,b,ra,k;intID; BOOL operator< (ConstDaynode & T)Const        {returnk<T.K;}} A[MAXN];structpointnode{Doublex, y;} P[MAXN];intN;DoubleDP[MAXN];intTmp[maxn],stack[maxn],top;#defineINF 1e9#defineEPS 1e-9DoubleSlopeintL1,intL2) {    if(l1==0)return-inf; if(l2==0)returninf; if(Fabs (p[l1].x-p[l2].x) <=eps)return-inf; return(P[L1].Y-P[L2].Y)/(p[l1].x-p[l2].x);}voidCDQ (intLintR) {    if(l==R) {Dp[l]=max (dp[l],dp[l-1]); P[l].y=dp[l]/(A[l]. RA*A[L]. A +A[l].            B); p[l].x=A[L]. ra*p[l].y; return; }    intMid= (l+r) >>1, L,r; L=l; R=mid+1;  for(intI=l; i<=r; i++)        if(A[i].id<=mid) tmp[l++]=i;Elsetmp[r++]=i;  for(intI=l; i<=r; i++) p[i]=P[tmp[i]];    CDQ (L,mid); Top=0;  for(intI=l; i<=mid; i++)        {             while(top>1&& Slope (stack[top],stack[top-1]) <slope (i,stack[top-1]) +eps) top--; stack[++top]=i; }    inttop=1;  for(intk=0; I>=mid+1; i--)        {             while(Top<top && Slope (stack[top],stack[top+1]) +EPS&GT;A[I].K) top++; Dp[a[i].id]=max (Dp[a[i].id],p[stack[top]].x*a[i]. a+p[stack[top]].y*A[i].        B); } CDQ (Mid+1, R); L=l,r=mid+1;  for(intI=l; i<=r; i++)        if(((P[l].x<p[r].x+eps | | (Fabs (p[l].x-p[r].x) <=eps && p[l].y<p[r].y+eps)) || R&GT;R) && l<=mid) Tmp[i]=l++; Elsetmp[i]=r++;  for(intI=l; i<=r; i++) p[i]=P[tmp[i]];}intMain () {scanf ("%D%LF", &n,&dp[0]);  for(intI=1; i<=n; i++) scanf ("%LF%LF%LF", &a[i]. A,&a[i]. B,&a[i]. Ra), A[i].k=-a[i]. A/a[i]. B,a[i].id=i; Sort (a+1, a+n+1); CDQ (1, N); //for (int i=1; i<=n; i++) printf ("%d%d%.3lf%.3lf%.3lf%.3lf \ n", I,a[i].id,a[i]. A,a[i]. B,a[i]. RA,A[I].K);printf"%.3lf\n", Dp[n]); return 0;}
View Code

"BZOJ-1492" Currency exchange cash DP + slope optimization + CDQ Division

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.