#P2687. Stock Purchase Strategy: Buy at Lower Prices
Stock Purchase Strategy: Buy at Lower Prices
Stock Purchase Strategy: Buy at Lower Prices
The key to success in stock trading, as the old saying goes, is to buy low. In this problem, you are given the stock prices for N consecutive days. You can buy a stock on any day you want; however, when you buy, the price on that day must be strictly lower than the price on the day you made your previous purchase. Your task is to determine the maximum number of times you can purchase stocks following this rule.
Note: The first purchase can be made on any day. For every subsequent purchase, the price must be lower than the last price at which you bought a stock.
For example, consider the following table of prices:
Day | Price |
---|---|
1 | 68 |
2 | 69 |
3 | 54 |
4 | 64 |
5 | 68 |
6 | 64 |
7 | 70 |
8 | 67 |
9 | 78 |
10 | 62 |
11 | 98 |
12 | 87 |
One possible way to make the purchases is:
Day | Price |
---|---|
2 | 69 |
5 | 68 |
6 | 64 |
10 | 62 |
Here, you are able to purchase stocks 4 times following the rule. Your task is to write a program that calculates the maximum number of purchases possible given the stock prices over N days.
The condition for each purchase (after the first) can be formally written in LaTeX as follows:
$$price_{i} > price_{j} \quad\text{for\ each\ consecutive\ purchase\ where}\ i < j. $$inputFormat
The first line contains an integer N indicating the number of days.
The second line contains N space-separated integers representing the stock prices on each day.
outputFormat
Output a single integer representing the maximum number of times you can purchase stocks by following the rule that each purchase (after the first) must be made at a strictly lower price than the previous purchase.
sample
12
68 69 54 64 68 64 70 67 78 62 98 87
4