Buy low, Buy lower
Time limit:1000 ms |
|
Memory limit:30000 K |
Total submissions:8327 |
|
Accepted:2888 |
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:
"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 shoshould 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 12Price 68 69 54 64 68 64 70 67 78 62 98 87
The best investors (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 10Price 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 should t 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 wocould 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 cocould produce the same string of decreasing prices and be counted as only a single solution.
Sample Input 1268 69 54 64 68 64 70 67 78 6298 87 Sample output 4 2 Source Usaco 2002 February |
Question:
The longest descending subsequence + the longest count of different subsequences.
Ideas: (post to others)
Note: repeated operations are counted only once.
How to remove some duplicates is the key to this question
Deduplication:
7
5 3 7 6 3 2 1
6
5 3 7 3 1
5
5 3 2 1 3
When the first group is pushed to the number 2, 3 will be repeated. Obviously, the first 3 is dispensable.
The same applies to the second group. The first 3 is dispensable.
1. If the longest descent sequence contains the next 3, such as the first and second sets of data, the first 3 is useless. After pushing the last 3, the previous repeated counting array is cleared.
2. if the longest descent sequence contains only the first 3, such as the third group of data, the operations in Case 1 will not affect the results, because the status of the first 3 in the third group has been pushed to 2 before being cleared.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#define maxn 35#define MAXN 100005#define mod 100000000#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;int n,m,ans,cnt,tot,k;int dp[5005],num[5005],a[5005];int main(){ int i,j,t; while(~scanf("%d",&n)) { for(i=1; i<=n; i++) { scanf("%d",&a[i]); a[i]=-a[i]; } ans=0; for(i=1;i<=n;i++) { t=0; for(j=1;j<i;j++) { if(a[i]>a[j]) { if(t<dp[j]) { t=dp[j]; num[i]=num[j]; } else if(t==dp[j]) { num[i]+=num[j]; } } else if(a[i]==a[j]) { num[j]=0; } } if(t==0) num[i]=1; dp[i]=t+1; if(ans<dp[i]) ans=dp[i]; } int res=0; for(i=1;i<=n;i++) { if(dp[i]==ans) res+=num[i]; } printf("%d %d\n",ans,res); } return 0;}/*44 1 3 1352 52 5164 3 4 1 3 142 2 2 2*/