Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions:11148 | Accepted: 3920 |
Description
"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 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 10 11 12
Price 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 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
Input
* Lines 2..etc: A series of N space-separated integers, ten per line except the final line which might have fewer integers.
Output
* 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 would 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 could produce the same string of decreasing prices and be counted as only a single solution.
Sample Input
12 68 69 54 64 68 64 70 67 78 62 98 87
Sample Output
4 2
Source
题意:
其实就是一个最长下降子序列。并且要求输出方案数。
思路:
最长下降子序列好做 dp[i]表示以i为结尾的子串的最长下降子序列长度。
难点是方案数。如果可以考虑重复的子序列的话也比较简单,如果i是由j转移来的,方案数加上j的方案数就可以了。
但是题目中需要考虑去掉重复的个数。
如果在前面找到了某个数和当前的i的stock是相同的话,就不用再重复算前面的了。比如一个k < j < i,stock[j] = stock[i],k可以转移到j和i。如果 j~i之间有一个数m,可以转移到i,那么stock[m] > stock[i],且由于stock[j] = stock[i],stock[k] > stock[j],所以一定有stock[k] > stock[m]
啊啊啊啊啊编不下去了,想不通。
好了打电话给zcx已经经过点拨想通了。真开心。
我们假设j < i并且stock[j] = stock[i],并假设dp[i]就是这个最大的答案。如果dp[i] > dp[j], 那么说明,j+1~i-1中一定还有数在这个子序列中,dp[i]是由j+1~i-1中的某个数转移来的。dp[i]不可能小于dp[j], 最多是相等。当他们相等时,统计答案的时候分别加上就行了,也就是53行到57行的作用。
【日常感谢zcx大恩大德】
1 //#include <bits/stdc++.h> 2 #include<iostream> 3 #include<cmath> 4 #include<algorithm> 5 #include<stdio.h> 6 #include<cstring> 7 #include<vector> 8 #include<map> 9 10 #define inf 0x3f3f3f3f 11 using namespace std; 12 typedef long long LL; 13 14 int n; 15 const int maxn = 5005; 16 int stock[maxn], sum[maxn]; 17 int dp[maxn]; 18 19 20 int main() 21 { 22 while(scanf("%d", &n) != EOF){ 23 for(int i = 1; i <= n; i++){ 24 scanf("%d", &stock[i]); 25 26 } 27 28 //memset(dp, -inf, sizeof(dp)); 29 stock[0] = inf; 30 int cnt = 0; 31 for(int i = 1; i <= n; i++){ 32 for(int j = 0; j < i; j++){ 33 if(stock[j] > stock[i]){ 34 dp[i] = max(dp[i], dp[j] + 1); 35 } 36 } 37 } 38 39 sum[0] = 1; 40 for(int i = 1; i <= n; i++){ 41 for(int j = i - 1; j >= 0; j--){ 42 if(stock[i] < stock[j] && dp[j] + 1 == dp[i]){ 43 sum[i] += sum[j]; 44 } 45 if(stock[i] == stock[j])break; 46 } 47 } 48 49 int ans = 0, anssum = 0; 50 for(int i = 1; i <= n; i++){ 51 ans = max(dp[i], ans); 52 } 53 for(int i = 1; i <= n; i++){ 54 if(dp[i] == ans){ 55 anssum += sum[i]; 56 } 57 } 58 59 printf("%d %d\n", ans, anssum); 60 } 61 62 return 0; 63 }