P1108 low-cost purchase
Label Dynamic Plan
Difficulty Improve +/-
Title Description
"low-cost purchase" The proposal is half the rule of success in the dairy market. To be considered a great investor, you must follow the following questions to suggest: "Buy at a low price, then buy at a lower price". Every time you buy a stock, you must buy it at a price lower than the one you bought it last. The more times you buy, the better! Your goal is to ask for the maximum number of shares you can buy if you follow the above suggestions. You will be given a daily sale of a stock for a period of time (a positive integer within the 2^16 range), and you can choose which days to buy the stock. Each purchase must be guided by the principle of "low-cost purchase, then low-cost purchase". Write a program to calculate the maximum number of purchases.
Here is a price list for a particular stock:
Date 1 2 3 4 5 6 7 8 9 three-page
Price------4--- One of the line scenarios is:
Date 2 5 6
Price of 5000:
Input and output format
Input format:
Line 1th: N (1 <= n <=), Stock issue days
2nd line The number of n is the daily stock price.
Output format:
Output file contains only two digits: Maximum purchase count and number of scenarios with maximum number of purchases (<=2^31) These 2 scenarios are considered identical when two scenarios "look the same" (that is, they constitute the same price queue).
Input and Output sample
Input Sample # #:
Buylow. In
The 98 of the
Output sample # #:
Buylow. Out
4 2
/* First question is bare longest descent sub-sequence problem.
The second one asks for the number of tectonic schemes for the longest descent subsequence.
The S[i].tot represents the longest ascending subsequence length to I.
S[i].x represents the number of scenarios to the longest ascending subsequence of I.
Eg:4 2 2 1.
The longest is two 4 2 1 but such a scheme can only be counted once.
Obviously the rear is better than the front.
In this case, to avoid repeating the calculation.
We set the front s[].x to 0.
If S[i].tot==s[j].tot+1&&a[j]>a[i] (j<i) i.e. I is the continuation of the J sequence we calculate the s[i].x+=s[j].x by the counting principle. Finally, only the number of scenarios for the tail of a legitimate subsequence can be calculated.
*/#include <iostream> #include <cstdio> #define MAXN 5001 using namespace std;
int A[maxn],n,ans,tot; struct Data{int tot,x;}
S[MAXN];
int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |
Ch> ' 9 ') {if (ch== '-') Ch=getchar ();}
while (ch>= ' 0 ' &&ch<= ' 9 ') X=x*10+ch-48,ch=getchar ();
return x*f;
} int main () {n=read ();
for (int i=1;i<=n;i++) a[i]=read ();
for (int i=1;i<=n;i++) {s[i].tot=1;
for (int j=1;j<=i-1;j++) if (A[i]<a[j]) S[i].tot=max (s[i].tot,s[j].tot+1);
if (s[i].tot==1) s[i].x=1; for (int j=1;j<=i-1;j++) if (S[i].tot==s[j].tot+1&&a[j]>a[i]) s[i].x+=s[j].x;
else if (A[j]==a[i]&&s[i].tot==s[j].tot) s[j].x=0;
Ans=max (Ans,s[i].tot);
} for (int i=1;i<=n;i++) if (Ans==s[i].tot) tot+=s[i].x;
printf ("%d%d", ans,tot);
return 0; }