Buy low, buy LOWER
Time Limit: 1000MS |
|
Memory Limit: 30000K |
|
|
|
Description
The advice to ' buy low ' is half the formula-success in the ' bovine stock market. To is 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 your 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'll 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 all of the days. Each time you choose to buy, the price must is strictly lower than the previous time you bought the stock. Write a program which identifies which days your should 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 Ten 12Price 68 69 54 64 68 64 70 67 78 62 98 87
The best investor (by this problem, anyway) can buy at most four times if each purchase are lower then the previous Purchas E. One four day sequence (there might was others) of acceptable buys is:
Day 2 5 6 10Price 69 68 64 62
Input
* Line 1:n (1 <= N <=), the number of th for which stock prices is given
* Lines 2..etc:a series of N space-separated integers, ten per line except the final line which might has fewer integers .
Output
Integers on a single line:
* The length of the longest sequence of decreasing prices
* The number of sequences that has this length (guaranteed to fit in bits)
In counting the number of solutions, the potential solutions is considered the same (and would only count as one solution If they repeat the same string of decreasing prices, that's, if they "look at the same" when the successive prices was COM Pared. Thus, different sequence of "buy" days could produce the same string of decreasing prices and is counted as only a sin GLE solution.
Sample Input
1268 69 54 64 68 64 70 67 78 6298 87
Sample Output
4 2
Test instructions: gives the number of n, the length of the longest monotonically decreasing subsequence, and the length of the number of sequences L.
Note: 5 3 1, 5 3 1 a scenario.
Analysis: Because n does not exceed 5000, the O (n^2) complexity is acceptable. Then we can according to the usual method to find the longest monotone descending subsequence, when asked to add an auxiliary array cnt[i] records from the beginning to the first position, the monotonically decreasing subsequence is dp[i] the number of scenarios.
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int N = 1e4 + 10;int a[ N], Dp[n], Cnt[n];int main () {int N; while (~SCANF ("%d", &n)) {for (int i = 0; i < n; i++) {scanf ("%d", &a[i]); Dp[i] = 1; Cnt[i] = 1; } for (int i = 1, i < n; i++) {for (int j = i-1; J >= 0; j--) {if (A[i] < a[j]) {if (Dp[i] < dp[j] + 1) {//First find the element that can make the length grow dp[i] = Dp[j] + 1; Cnt[i] = Cnt[j]; } else if (dp[i] = = Dp[j] + 1)//has found an element that makes dp[i] = Dp[j] +1 and now finds a cnt[i] + = Cnt[j]; } else {if (a[i] = = A[j]) {if (dp[i] = = 1)//Replace the number on the J position with the number in the first position Word, forming the sequence exactly the same cnt[i] = 0; Break } } }} int max_len = 0, ans_cnt = 0; for (int i = 0; i < n; i++) Max_len = max (Max_len, Dp[i]); for (int i = 0; i < n; i++) if (dp[i] = = Max_len) ans_cnt + = Cnt[i]; printf ("%d%d\n", Max_len, ans_cnt); } return 0;}
POJ 1552 buy low, buy LOWER (maximum number of monotonically decreasing sub-sequence solutions)