Buy Low, Buy Lower
The advice to "buy low" is half the formula to success in the stock market. But to be considered a great investor you must also follow this problems' advice:
"Buy low, buy lower"
That is, each time you buy a stock, you must purchase more 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 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 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.
By way of example, suppose on successive days stock is selling like this:
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 87In the example above, the best investor (by this problem, anyway) can buy at most four times if they purchase at a lower price each time. One four day sequence (there might be others) of acceptable buys is:
Day 2 5 6 10 Price 69 68 64 62Two integers on a single line:
the length of the longest sequence of decreasing pricesthe number of sequences that have this lengthIn 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.
/* ID:cqz15311 LANG:C++ PROG:buylow */ #include<cstdio> #include<cmath> #include<algorithm> #include<iostream> #include<cstring> using namespace std; typedef long long LL; const int maxn = 5333; 从上面抄下来…… …… …… …… GJD num[maxn]; int a[maxn],dp[maxn],n; bool used[maxn]; struct Y{ int val,id; bool operator < (const Y &x) const{ return val < x.val; } }y[maxn]; int main(){ freopen("buylow.in","r",stdin); freopen("buylow.out","w",stdout); scanf("%d",&n); for (int i=1;i<=n;i++){ scanf("%d",&a[i]); y[i] . val = a[i]; y[i] . id = i; } sort(y+1,y+1+n); int cnt = 0; for (int i=1;i<=n;i++){ if (i == 1 || y[i] . val != y[i-1] . val){ a[y[i] . id] = ++cnt; } else a[y[i] . id] = cnt; } a[0] = n+1; a[++n] = -1; num[0] .clear(1);dp[0] = 0; memset(used,false,sizeof(used)); for (int i=1;i<=n;i++){ dp[i] = 1; for (int j=i-1;j>=0;j--){ if (a[i] < a[j]){ if (dp[j] + 1 > dp[i]) dp[i] = dp[j] + 1; } } for (int j=i-1;j>=0;j--){ if ((a[j] > a[i]) && (dp[i] == (dp[j]+1))){ if (!used[a[j]]){ num[i] = num[i] + num[j]; used[a[j]] = true; } } } for (int j=i-1;j>=0;j--) used[a[j]] = false; } printf("%d ",dp[n]-1); num[n] . write(); puts(""); fclose(stdin); fclose(stdout); return 0; } /* Executing... Test 1: TEST OK [0.000 secs, 12620 KB] Test 2: TEST OK [0.000 secs, 12620 KB] Test 3: TEST OK [0.000 secs, 12620 KB] Test 4: TEST OK [0.000 secs, 12620 KB] Test 5: TEST OK [0.000 secs, 12620 KB] Test 6: TEST OK [0.014 secs, 12620 KB] Test 7: TEST OK [0.000 secs, 12620 KB] Test 8: TEST OK [0.000 secs, 12620 KB] Test 9: TEST OK [0.042 secs, 12620 KB] Test 10: TEST OK [0.308 secs, 12620 KB] All tests OK. Your program ('buylow') produced all correct answers! This is your submission #2 for this problem. Congratulations! */