Buy low, buy LOWER--POJ_1952 -- longest descending subsequence

Source: Internet
Author: User
Tags stock prices

Question address: Id = 1952

Buy low, Buy lower
Time limit:1000 ms   Memory limit:30000 K
Total submissions:6692   Accepted:2302


The advice to "buy low" is half the formula to success in the bovine stock market. To be considered a great investor you must also follow this problems 'advice:
"Buy low; buy lower"

Each time you buy a stock, you must purchase it at a lower price than the previous time you bought it. The more times you buy at a lower price than before, the better! Your goal is to see how many times you can continue purchasing at ever lower prices.

You will be given the daily selling prices of a stock (positive 16-bit integers) over a period of time. you can choose to buy stock on any of the days. each time you choose to buy, the price must be strictly lower than the previous time you bought stock. write
A program which identifies which days you shoshould buy stock in order to maximize the number of times you buy.

Here is a list of stock prices:

Day 1 2 3 4 5 6 7 8 9 10 11 12 price 68 69 54 64 68 64 70 67 78 62 98 87

The best investors (by this problem, anyway) can buy at most four times if each purchase is lower then the previous Purchase. one four day sequence (there might be others) of acceptable buys is:

Day 2 5 6 10 Price 69 68 64 62


* Line 1: n (1 <= n <= 5000), the number of days for which stock prices are given

* Lines 2.. etc: A series of n space-separated integers, ten per line should t the final line which might have fewer integers.


Two integers on a single line:
* The length of the longest sequence of decreasing prices
* The number of sequences that have this length (guaranteed to fit in 31 bits)

In counting the number of solutions, two potential solutions are considered the same (and wocould only count as one solution) if they repeat the same string of decreasing prices, that is, if they "look the same" when the successive prices are compared. thus,
Two different sequence of "buy" Days cocould produce the same string of decreasing prices and be counted as only a single solution.

Sample Input

1268 69 54 64 68 64 70 67 78 6298 87

Sample output

4 2


Usaco 2002 February The question is, when you buy a stock, the price of the stock you bought this time must be strictly lower than that of the previous one. The more times you buy, the better. Now I will give you some stock. One per day. There are n days. Let you find the maximum number of stocks you can buy in this order. In addition, you also need to provide the maximum number of different options for buying stocks. If the descent sequence in the two schemes is the same, it can only be regarded as one. Now we need to provide the longest length and the number of different solutions with the longest length. Use F [I] to indicate the length of the longest descending subsequence ending with nth I, use count [I] to indicate the number of different schemes with the length of F [I] ending with the I. Use Data [I] to save each value. The state transition equation is: F [I] = max (F [J] + 1) 0 <= j <I & Data [J]> data [I] Count [I] = sum (count [J]) 0 <= j <I & F [I] = f [J] + 1 & Data [J]> data [I] It should be noted that If f [I] = f [J] & Data [I] = data [J], they are at the same position and value in the descent subsequence, you only need to calculate the first of them, and the rest do not need to be included. In this case, we need to set data [0] To a large number, and the other numbers will decrease relative to it.

# Include <iostream> using namespace STD; # define Max 5050 long f [Max]; // F [I] indicates the length of long data [Max]; Long Count [Max]; // number of schemes with the I-th element as the last character and the length of F [I] Being different to the longest decreasing subsequence int main () {int N; while (CIN> N & N) {int I, j; int max = 0; for (I = 1; I <= N; I ++) {CIN> data [I]; // assign an initial value. Because no data exists at the beginning, 0 f [I] = 0 is assigned; count [I] = 0;} f [0] = 0; // calculates the maximum decreasing Subsequence data [0] = 999999999; for (I = 1; I <= N; I ++) {for (j = I-1; j> = 0; j --) if (data [J]> data [I] & F [J] + 1> F [I]) f [I] = f [J] + 1; if (max <F [I]) // save max = f [I];} //////////////////////////////////////// count, it is also a DP, and the conversion equation is // count [I] = All (count [J]) & DP [I] = DP [J] + 1 & Data [I] <data [J] // remove elements with the same position and data in the sequence, I used the removal method int acount = 0; count [0] = 1; for (I = 1; I <= N; I ++) {for (j = I-1; j> = 0; j --) {If (F [I] = f [J] & Data [I] = data [J]) // if the location is consistent with the data, the break is not repeated; if (F [I] = f [J] + 1 & Data [I] <data [J]) Count [I] + = count [J];} if (F [I] = max & COUNT [I]) // reaches the length of acount + = count [I]; // cout <"--------------" <Endl; // cout <I <"" <count [I] <Endl ;} cout <max <"" <acount <Endl;} return 0 ;}

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: 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.