Bzoj 1492: [NOI2007] Currency exchange Cash (DP + balance tree)

Source: Internet
Author: User

DP (i) = Max (DP (I-1), X[j]*a[i]+y[j]*b[i]), 0<j<i. x, y means the maximum amount of money you have to buy a gold coupon, a sum of gold coupons, and B. And then it's obvious ... The balance tree is maintained on the convex hull, when asked for two points on the convex hull ... Time complexity O (NLOGN)

-----------------------------------------------------------------------------------------------

#include <cmath>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>using namespace std; #define K (A, B) ((A.Y-B.Y)/(a.x-b.x))const int MAXN = 100009;Const DOUBLE EPS = 1e-7;Const Double INF = 1e100;double DP, A, B, R, K;int N;struct P {double x, y;P (Double _x = 0, double _y = 0): X (_x), Y (_y) {}BOOL Operator < (const P &p) Const {return x < p.x;}bool operator = = (CONST P &p) Const {return Fabs (x-p.x) < EPS && fabs (Y-P.Y) < EPS;}} p;struct Node {node* ch[2];double lk, RK;int R;p p;} POOL[MAXN], *pt, *root, *null;void Init_treap () {pt = pool;pt->ch[0] = pt->ch[1] = pt;pt->p = P (-inf,-inf);Root = Null = pt++;}void Rotate (node*&t, int d) {node* o = t->ch[d ^ 1];t->ch[d ^ 1] = o->ch[d];o->ch[d] = t;t = o;} void Insert (node*&t) {if (t = = Null) {(t = pt++)->p = p, t->r = rand ();t->ch[0] = t->ch[1] = Null;} else {int d = (T->p < p);Insert (T->ch[d]);if (T->ch[d]->r > T->r) Rotate (T, D ^ 1);}}void Delete (node*&t) {int d = (p = = t->p?-1: (T->p < P));if (d = =-1) {if (t->ch[0]! = NULL && t->ch[1]! = null) {int _d = (T->ch[0]->r > t->ch[1]->r);Rotate (T, _d), Delete (T->ch[_d]);} elset = (t->ch[0]! = Null? T->ch[0]: t->ch[1]);} elseDelete (T->ch[d]);}node* Pred (P &p) {node* ret = Null;For (node* o = Root; O! = Null;) if (O->p < p)ret = o, o = o->ch[1];Elseo = o->ch[0];return ret;}node* Succ (P &p) {node* ret = Null;For (node* o = Root; O! = Null;) if (P < o->p)ret = o, o = o->ch[0];Elseo = o->ch[1];return ret;}node* Find (P &p) {For (node* t = Root; t! = Null;) {if (fabs (t->p.x-p.x) < EPS) return t;t = (p.x < t->p.x t->ch[0]: t->ch[1]);}return 0;}P Select (node*&t) {if (t->r = =-1) return Select (t->ch[1]);if (t->r = =-2) return Select (T->ch[0]);if (K-t->lk < EPS && t->rk-k < EPS) return t->p;return k-t->lk > EPS? Select (T->ch[0]): Select (T->ch[1]);}void Init () {init_treap ();p = P (0,-inf), Insert (Root);p = P (1e10,-inf), Insert (Root);node* t = pt;(--t)->r =-2, (--t)->r =-1;}void Add () {Double b = DP/(A * R + B), a = b * r;p o = P (A, b);Node *t = Find (o);if (t) {if (T->p.y-o.y > EPs) return;p = t->p, Delete (Root);}Node *l = Pred (o), *r = SUCC (o);if (r->p = = O | | K (o, R->p)-K (o, l->p) > EPs) return;For (node* LL = Pred (l->p); LL! = Null; ) {if (k (O, L->p)-K (l->p, Ll->p) > EPS)p = l->p, Delete (Root);ElseBreak ;L = ll, LL = Pred (l->p);}pt->lk = L->rk = K (l->p, O);For (node* RR = SUCC (r->p); RR! = Null; ) {if (K (rr->p, R->p)-K (r->p, O) > EPS)p = r->p, Delete (Root);ElseBreak ;R = RR, rr = SUCC (r->p);}r->lk = Pt->rk = K (r->p, O);p = o, Insert (Root);} void Work () {scanf ("%d%lf", &n, &DP);for (int i = 0; i < N; i++) {scanf ("%lf%lf%lf", &a, &b, &r);if (i) {K =-a/b;P o = Select (Root);DP = MAX (DP, A * o.x + B * o.y);}Add ();}printf ("%.3lf\n", DP);} int main () {Init ();Work ();return 0;}

-----------------------------------------------------------------------------------------------

1492: [NOI2007] Currency exchange cash time limit: 5 Sec Memory Limit: MB
Submit: 2843 Solved: 1201
[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 Output225.000HINT



The test data is designed so that the accuracy error does not exceed 10-7.
For 40% of test data, meet n≤10;
For 60% of test data, meet N≤1 000;
For 100% of test data, meet N≤100 000;

Source

Bzoj 1492: [NOI2007] Currency exchange Cash (DP + balance tree)

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.