Title Description
"Cheap buy" This recommendation is half the success of the rules of the stock market in dairy cows. To be considered a great investor, you have to follow the following question advice: "Buy cheap; cheap to buy again." Every time you buy a stock, you have to use less than the last time you buy it the price to buy it. The number of buy, the better! Your goal is to follow the above recommendations under the premise of seeking the number of times you can purchase up stocks. You will be given a stock sale price (per day over a period of time 2 ^ {16} 2 . 1 a positive integer in the range of 6), you can select on which days later the stock. Each purchase must follow the "buy low; and then buy low-cost" principle. Write a program that calculates the maximum number of purchases.
Here is a stock's price list:
Date 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 1 0 , 1 1 , 1 2
Price 68, 69, 54, 64, 68, 64, 70, 67, 78, 62, 98, 87 . 6 . 8 , . 6 . 9 , . 5 . 4 , . 6 . 4 , . 6 . 8 , . 6 . 4 , . 7 0 , . 6 . 7 , . 7 . 8 , . 6 2 , . 9 . 8 , . 8 . 7
The best investors can purchase up to 4 four shares, one possible scenario is:
Date of 2, 5, 6, 10 2 , 5 , 6 , . 1 0
Price 69, 68, 64 and 62 . 6 . 9 , . 6 . 8 , . 6 . 4 , . 6 2
Input Format
Line 1: N (1 \ Le N \ Le 5000) N ( 1 ≤ N ≤ 5 0 0 0 ), the number of days the stock issuance
Line 2: N number N, a daily stock price.
Output Format
Two numbers:
the maximum number of purchases and the purchase has the largest number of the program ( \ Le ^ {2} 31 is ≤ 2 . 3 . 1) when the two kinds of programs "look the same" time (that is the same price when they form a queue) this 2 two kinds of programs is considered to be the same.
Sample input and output
12 68 69 54 64 68 64 70 67 78 62 98 87
4 2
Solution: from Los Valley, Super (blue) of a DP title
#include <bits/stdc++.h> using namespace std; const int maxn = 5005; int n, a[maxn]; int dp[maxn]; int f[maxn]; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; for(int i = 1; i <= n ; i++) cin >> a[i]; int ans1 = 0, ans2 = 0; for(int i = 1; i <= n; i++) { dp[i] = 1; for(int j = 1; j < i; j++) if(a[i] < a[j]) { dp[i] = max(dp[i], dp[j] + 1); } ans1 = max(ans1, dp[i]); } for(int i = 1; i <= n; i++) { if(dp[i] == 1) f[i] = 1; for(int j = 1; j < i; j++) if(dp[i] == dp[j] + 1 && a[i] < a[j]) f[i] += f[j]; else if(dp[i] == dp[j] && a[i] == a[j]) f[i] = 0; if(dp[i] == ans1) ans2 += f[i]; } cout << ans1 << " " << ans2 << endl; }