USACO 4.3 Buy Low, Buy Lower

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 87

In 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 62

PROGRAM NAME: buylow

INPUT FORMAT

Line 1: N (1 <= N <= 5000), the number of days for which stock prices are available.
Line 2..etc: A series of N positive space-separated integers (which may require more than one line of data) that tell the price for that day. The integers will fit into 32 bits quite nicely.

SAMPLE INPUT (file buylow.in)

12
68 69 54 64 68 64 70 67
78 62 98 87

OUTPUT FORMAT

Two integers on a single line:

  • the length of the longest sequence of decreasing prices
  • the number of sequences that have this length

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 OUTPUT (file buylow.out)

4 2————————————————————————————————————————————————————————————这道题乍一看似乎很水然而……好吧最长下降子序列可以n^2的求出但是我们还需要序列个数用加法原理假如遇到a[j]>a[i](j<i) 且f[j]+1==f[i]我们就给计数器d数组d[i]+=d[j]但这样肯定不是最后的答案,因为每个数的数字需要不一样,而不是下标不一样用一个next数组记录距离j最近的一个和a[j]相等的数的下标,如果这个下标在i之前我们可以跳过j,如果没有或这个下标在i之后再进行计算,这样的话我们就可以囊括之前的情况并且避免重复计算了在序列最后加一个0,最终答案就可以很容易的聚到最后一位上了还有高精qwq【是时候认认真真写个高精当模板了qwq】
1 /*
 2 ID:ivorysi
 3 PROG:buylow
 4 LANG:C++
 5 */
 6 #include <iostream>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <queue>
10 #include <set>
11 #include <vector>
12 #define inf 0x7fffffff
13 #define ivorysi
14 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
15 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
16 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
17 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
18 using namespace std;
19 struct bignum {
20     vector<int> s;
21     bignum operator =(int x) {
22         s.clear();
23         do {
24             s.push_back(x%10);
25             x/=10;
26         }while(x>0);
27         return *this;
28     }
29     bignum operator + (const bignum &b) const {
30         bignum c;
31         c.s.clear();
32         for(int g=0,k=0;;++k) {
33             if(g==0 && k>=b.s.size() && k>=s.size()) {break;}
34             int x=g;
35             if(k<b.s.size()) x+=b.s[k];
36             if(k<s.size()) x+=s[k];
37             c.s.push_back(x%10);
38             g=x/10;
39         }
40         return c;
41     }
42     bignum &operator +=(const bignum &b)  {
43         *this=*this+b;
44         return *this;
45     }
46 }d[5005];
47 ostream& operator << (ostream &out, const bignum& x) {   
48   gongzi(i,x.s.size()-1,0) {   
49     out<<x.s[i];
50   }  
51   return out;  
52 }  
53 int n,a[5005],f[5005],ans,ans1,next[5005];
54 void solve() {
55     scanf("%d",&n);
56     siji(i,1,n) scanf("%d",&a[i]);
57     ++n;
58     a[n]=0;
59     siji(i,1,n) {f[i]=1;d[i]=0;}
60     siji(i,1,n) {
61         siji(j,i+1,n) {
62             if(a[i]>a[j]) {
63                 if(f[i]+1>f[j]) {
64                     f[j]=f[i]+1;
65                 }
66             }
67         }
68     }
69     siji(i,1,n) {
70         siji(j,i+1,n) {
71             if(a[i]==a[j]) {next[i]=j;break;}
72         }
73     }
74     siji(i,1,n) {
75         if(f[i]==1) d[i]=1;
76         xiaosiji(j,1,i) {
77             if(a[j]>a[i]) {
78                 if(f[j]+1==f[i] && (next[j]==0 || next[j]>i)) {
79                     d[i]=d[i]+d[j];
80                 } 
81             }
82         }
83     }
84     printf("%d ",f[n]-1);
85     cout<<d[n]<<endl;
86 }
87 int main(int argc, char const *argv[])
88 {
89 #ifdef ivorysi
90     freopen("buylow.in","r",stdin);
91     freopen("buylow.out","w",stdout);
92 #else
93     freopen("f1.in","r",stdin);
94 #endif
95     solve();
96     return 0;
97 }


原文链接: https://www.cnblogs.com/ivorysi/p/6297897.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/248039

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 上午2:38
下一篇 2023年2月14日 上午2:39

相关推荐