Hdu3401 (DP + monotonous Queue)

Source: Internet
Author: User

Question:

A person has t yuan, and the maximum number of shares that can be held is maxp. The transaction must be conducted every w days. tell the number and price of stocks that can be bought every day, the number and price of stocks that can be sold every day, and ask what is the maximum benefit at the end.

Ideas:

The status can be defined as: F [I] [J] day I, the maximum benefit of holding J shares and completing the operation of the day. there are three types of operations to consider this day: (1) Do nothing, (2) One sales transaction on this day, and (3) one purchase transaction on this day. that is, F [I] [J] = max (F [I-1] [J], F [i-w-1] [k] + (J-k) * BPI, f [i-w-1] [k]-(k-j) * API)

Two questions.

First, why do we only consider W days between the current day and the current day, at least W days, or even w + X days. let's assume that we consider W, W + 1, W + 2, W + 3... in fact, when we consider the W + 1 day, we have considered the form of nothing to do on this day, that is to say, we have considered the form of F [I-1] [J], so every day we consider f [I-1] [J], we do not need to consider every interval greater than W. because you have already considered it.

Second, it seems that the number of States is O (2000*2000), and the transfer number (enumeration K) is O (2000). The overall complexity is (8*10 ^ 9) unacceptable. how to optimize it? If you can find the largest buy and sell status in O (1) time, you can accept it. status deformation. f [i-w-1] [k] + (J-k) * BPI = f [i-w-1] [k]-K * BPI + J * BPI. for the given request State f [I] [J], the following J * BPI is a constant, the front f [i-w-1] [k]-K * BPI the first one-dimensional i-w-1 is a constant for a given I, and the last one-dimensional k is less than the number of J. if we can quickly find the maximum value of F [i-w-1] [k]-K * BPI is OK. because as long as I are determined, the i-w-1 is uniquely identified, and the order of the solution is certain, then if the current I is determined, we determine whether it is greater than W + 1, if greater than, that is, f [i-w-1] [k] (k <j) is used in the process of enumerating every J ). in parallel with j, k is enumerated first, and the maximum value is maintained with the monotonic queue. Then, during the enumeration of J, the monotonic queue is evenly distributed to O (1) the complexity can be quickly given for a specified J corresponding K makes f [i-w-1] [k] maximum.

AC code.

1 # include <iostream> 2 # include <cstdio> 3 # include <algorithm> 4 using namespace STD; 5 # define maxn 2010 6 # define INF 0x7fffffff 7 struct node {8 int X; // monotonic queue DP [i-w-1] [k] + API [I] * K or DP [i-w-1] [k] + BPI [I] * K 9 int P; // number of shares 10} que [maxn], temp; 11 int S, E, DP [maxn] [maxn]; 12 INT main () 13 {14 // freopen ("in.txt", "r", stdin); 15 int API [maxn], BPI [maxn], ASI [maxn], BSI [maxn], t, maxp, W, T, Res; 16 int I, j; 17 scanf ("% d", & T); 18 while (t --) 19 {20 res =-INF; 21 scanf ("% d", & T, & maxp, & W); 22 for (I = 1; I <= T; I ++) 23 scanf ("% d", & API [I], & BPI [I], & ASI [I], & BSI [I]); 24 for (I = 0; I <= T; I ++) 25 for (j = 0; j <= maxp; j ++) 26 dp [I] [J] =-INF; 27 for (I = 1; I <= W + 1; I ++) 28 For (j = 0; j <= ASI [I]; j ++) 29 DP [I] [J] =-API [I] * J; 30 For (I = 2; I <= T; I ++) 31 {32 for (j = 0; j <= maxp; j ++) 33 DP [I] [J] = max (DP [I] [J], DP [I-1] [J]); 34 if (I <= W + 1) continue; 35 // buy 36 S = E = 1; 37 for (j = 0; j <= maxp; j ++) 38 {39 temp. X = DP [i-W-1] [J] + J * API [I]; 40 temp. P = J; 41 for (; S <E & que [E-1]. x <temp. x; e --); 42 que [E ++] = temp; 43 for (; S <E & que [s]. P + ASI [I] <j; s ++); 44 DP [I] [J] = max (DP [I] [J], que [s]. x-API [I] * j); 45} 46 // sell 47 s = E = 1; 48 for (j = maxp; j> = 0; j --) 49 {50 temp. X = DP [i-W-1] [J] + J * BPI [I]; 51 temp. P = J; 52 for (; S <E & que [E-1]. x <temp. x; e --); 53 que [E ++] = temp; 54 for (; S <E & que [s]. p-BSI [I]> J; s ++); 55 DP [I] [J] = max (DP [I] [J], que [s]. x-BPI [I] * j); 56} 57} 58 for (I = 0; I <= maxp; I ++) 59 res = max (Res, DP [T] [I]); 60 cout <res <Endl; 61} 62 Return 0; 63}
View code

 

Hdu3401 (DP + monotonous Queue)

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.