BUY LOW, BUY LOWER
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 10466 | Accepted: 3657 |
Description
The advice to "buy low" is half the formula to success in the bovine stock market.To be considered a great investor you must also follow this problems' advice:
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:
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:
"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
* Line 1: N (1 <= N <= 5000), the number of days for which stock prices are given
* Lines 2..etc: A series of N space-separated integers, ten per line except the final line which might have fewer integers.
* Lines 2..etc: A series of N space-separated integers, ten per line except the final line which might have fewer integers.
Output
Two integers on a single line:
* 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.
* 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
题意:给出一个序列,求出最长下降子序列的方案数,方案不能重复
思路:
会出现重复的方案一定是存在重复的数字。先定义两个数组:dp[i]表示以第i位数字结尾能组成的最大长度、cnt[i]表示以第i位数字结尾组成最大长度的方案数。我们考虑每个i都从后第i位往前找大于第i位的数进行dp,当第j位出现了和第i位相等的数字那么前面和第j位数字组成的方案,一定也能和第i位数字组,这里就有重复了,所以我们可以直接跳出,不需要在遍历j位前面的数了,然后当j和i之间的数都不能和i连接的话,那i的贡献就和j是一样了,这里就把cnt[i]=0就不会重复计算了。具体按代码
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <list>
#include <vector>
#include <cstdlib>
#define INF 0x3f3f3f3f
#define MS(x,y) memset(x, y, sizeof(x))
#define MOD 1000000007
#define LL long long int
//#define TEST
using namespace std;
int z[5010], dp[5010], cnt[5010];
int N;
int main()
{
#ifdef TEST
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif // TEST
//std::ios::sync_with_stdio(false);
while(~scanf("%d", &N))
{
for(int i=0;i<N;++i)
{
scanf("%d", &z[i]);
dp[i] = cnt[i] = 1;
}
int MAXdp = 1;
int ans = 0;
for(int i=1;i<N;++i)
{
for(int j = i-1;j>=0;--j)
{
if(z[i] < z[j])
{
if(dp[i] < dp[j]+1)
{
dp[i] = dp[j]+1;
cnt[i] = cnt[j];
}
else if(dp[i] == dp[j]+1)
{
cnt[i] += cnt[j];
}
}
else if(z[i] == z[j])
{
if(dp[i] == 1)
{
cnt[i] = 0;
}
break;
}
}
MAXdp = max(MAXdp, dp[i]);
}
for(int i=0;i<N;++i)
{
if(dp[i] == MAXdp)
ans += cnt[i];
}
printf("%d %d\n", MAXdp, ans);
}
return 0;
}