P1108 low price purchase, p1108 low price purchase

Source: Internet
Author: User

P1108 low price purchase, p1108 low price purchase

The "buy at a low price" recommendation is half the rule of success in the dairy market. To be considered a great investor, you must follow the following questions and suggestions: "low price purchase; low price purchase again ". Every time you buy a stock, you must purchase it at a lower price than the price you purchased it last time. The more times you buy, the better! Your goal is to follow the above suggestions and ask you for the maximum number of times you can buy a stock. You will be given the daily sale price of a stock within a period of time (a positive integer in the range of 2 ^ 16), you can choose on which days to buy this stock. Each purchase must follow the principle of "low price purchase; then low price purchase. Write a program to calculate the maximum number of purchases.

Here is the price list of a stock:

Date 1 2 3 4 5 6 7 8 9 10 11 12

Price 68 69 54 64 68 70 67 78 62 98 87

The best investors can buy up to four shares. One of the feasible schemes is:

Date 2 5 6 10

Price 69 68 64 62

Input/Output Format Input Format:

Row 3: N (1 <= N <= 1st), stock issuance days

Row 2nd: N number, which is the stock price per day.

Output Format:

The output file contains only two rows: the maximum number of purchases and the maximum number of purchases (<= 2 ^ 31) when the two schemes "look the same" (that is, they make up the same price Queue), these two schemes are considered to be the same.

Input and Output sample Input example #1:
BUYLOW.IN1268 69 54 64 68 64 70 67 78 62 98 87
Output sample #1:

First, let's look at the example. The maximum number of purchases is 4. There are two solutions in total, namely 69 68 64 62 and 69 68 67 62.

We found that this question is actually to select a sequence in a series, so that this sequence is a descending sequence (that is, any number in the series must be greater than any number after it ), the length of the sequence must be the longest. However, to output the total number of solutions for this question, we need to make some changes to the original solution process. To solve the total number of solutions, we need to remove repeated solutions. When two or more of the N numbers in the 2nd rows are at the same price, there may be repeated solutions. When a recurring scheme is generated, it is obvious that the subsequent price is better than the previous one, because the total number of the longest descent sequence at the end of the subsequent price is certainly not less than the previous one, and the plan must cover all the schemes at the previous price. Therefore, in solving the problem, we can only consider the one behind the same price. If there is a duplicate status before the current status, we only need to consider the one closest to the current status.

If f [I] is set to day I, the maximum number of times that can be bought is obviously: f [1] = 1; f [I] = max {f [j] + 1} (1 <= j <= I-1, and OK [j] = 1 ), OK [j] = 1 indicates that this location is better when the price is the same.

1 # include <stdio. h> 2 # include <string. h> 3 bool OK [50010]; 4 int a [5010], B [5010], f [5010]; 5 int n, I, j, k, max, num; 6 int main () 7 {8 scanf ("% d", & n); 9 for (I = 1; I <= n; I ++) 10 scanf ("% d", & a [I]); 11 B [1] = 1; 12 f [1] = 1; 13 for (I = 2; I <= n + 1; I ++) 14 {15 max = 0; 16 f [I] = 1; 17 for (j = I-1; j> = 1; j --) 18 if (a [I] <a [j]) 19 if (B [j]> max) // B [j] indicates the maximum number of purchases ending on day j 20 {21 max = B [j]; 22 memset (OK, 1, sizeof (OK )); 23 OK [a [j] = 0; 24 f [I] = f [j]; 25} 26 else27 if (B [j] = max & OK [a [j]) 28 {29 OK [a [j] = 0; 30 f [I] + = f [j]; 31} 32 B [I] = max + 1; 33} 34 printf ("% d ", B [n + 1]-1, f [n + 1]); 35}


Related Article

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.