usaco 4.3 Buy Low, Buy Lower 2010.8.5

来源:互联网 发布:js什么是递归算法 编辑:程序博客网 时间:2024/05/01 03:15

让人抓狂的题。。。。

 

还好usaco给你提供出错的数据,不然。。。。。囧。。。。

 

Buy Low, Buy Lower

The advice to "buy low" is halfthe formula to success in the stock market. But to be considered a greatinvestor 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 theprevious time you bought it. The more times you buy at a lower price thanbefore, the better! Your goal is to see how many times you can continuepurchasing at ever lower prices.

 

You will be given the daily selling pricesof a stock over a period of time. You can choose to buy stock on any of thedays. Each time you choose to buy, the price must be lower than the previoustime you bought stock. Write a program which identifies which days you shouldbuy stock in order to maximize the number of times you buy.

 

By way of example, suppose on successivedays 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 (bythis problem, anyway) can buy at most four times if they purchase at a lowerprice each time. One four day sequence (there might be others) of acceptablebuys is:

 

Day   2  5  6 10

Price 69 68 64 62

PROGRAM NAME: buylow

INPUT FORMAT

Line 1: N (1 <= N <= 5000), thenumber of days for which stock prices are available.

Line 2..etc: A series of N positivespace-separated integers (which may require more than one line of data) thattell 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 ofdecreasing prices

the number of sequences that have thislength

In counting the number of solutions, twopotential solutions are considered the same (and would only count as onesolution) if they repeat the same string of decreasing prices, that is, if they"look the same" when the successive prices are compared. Thus, twodifferent sequence of "buy" days could produce the same string ofdecreasing prices and be counted as only a single solution.

 

SAMPLE OUTPUT (file buylow.out)

4 2

WA 因

 

1。价格序列相同的只算一次,米有处理好,我算了好多次,看我nocow的题解才知道,它的大概意思就是

 

5 4 3 2 3 1 此种情况,当计算“1”时,序列中的1前面 有2个3,第一个3,不予考虑;

 

2。处理好了相同序列的问题,但是,计数的结果,要用到高精度。。囧死。。。。

 

     把高精度调好了以后,发现超时。。。要压位高精

 

     压位高精以后,交上去不通过,原来是超内存了。

 

     把内存改小了,开始测试了,到test9 的时候,超时,把存结果的高精度数组的定义g[300]改成g[100]就行了~~~原来内存开打了也影响运行时间的。。。唉。。。


/*ID:PROG:buylowLANG:C++*/#include <cstdio>#include <cstring>using namespace std;#define MAXN 5005struct node{long long g[100];int len;}num[MAXN];long long a[MAXN],K=10000000000000000;int f[MAXN];int n,ans;int next[MAXN];int max(int x,int y){if (x>y)   return x;else   return y;}node add(node x,node y){node ans;memset(ans.g,0,sizeof(ans.g));int t=max(x.len,y.len);for(int i=1;i<=t;i++){   ans.g[i]+=x.g[i]+y.g[i];   ans.g[i+1]+=ans.g[i]/K;   ans.g[i]%=K;}ans.len=t;while (ans.g[(ans.len)+1]){   ans.g[ans.len+1]+=ans.g[ans.len]/K;   ans.g[ans.len]%=K;   ans.len=ans.len+1;}return ans;}void init(){   for(int i=1;i<=n;i++){   scanf("%lld",&a[i]);   f[i]=1;   memset(num[i].g,0,sizeof(num[i].g));   num[i].g[1]=1;   num[i].len=1;}}void nextit(){   for(int i=1;i<=n;i++){   for(int j=i+1;j<=n;j++)    if (a[i]==a[j])    {     next[i]=j;     break;    }}}int main(){freopen("buylow.in","r",stdin);freopen("buylow.out","w",stdout);memset(a,0,sizeof(a));memset(next,0,sizeof(next));scanf("%d",&n);    init();n++;a[n]=0;    nextit();// for(int i=1;i<=n;i++)//   printf("next   i %d      %lld      %d\n",i,a[i],next[i]);for(int i=1;i<=n;i++){   for(int j=1;j<i;j++)   {    if ((next[j]<i)&&(next[j]!=0))     continue;    if (a[j]>a[i])    {     if (f[j]+1==f[i])      num[i]=add(num[i],num[j]);     else      if(f[j]+1>f[i])      {       f[i]=f[j]+1;       num[i]=num[j];      }    }   }} //for(int i=1;i<=n;i++)   //printf("%d   %lld %d\n",i,a[i],f[i]);printf("%d ",f[n]-1);// printf("%d     len\n",num[n].len);printf("%lld",num[n].g[num[n].len]);for(int i=(num[n].len)-1;i>=1;i--)   printf("%016lld",num[n].g[i]);printf("\n");return 0;}

Compiling...

Compile: OK

 

Executing...

  Test 1: TEST OK [0.011 secs, 6896 KB]

  Test 2: TEST OK [0.000 secs, 6896 KB]

  Test 3: TEST OK [0.000 secs, 6896 KB]

  Test 4: TEST OK [0.000 secs, 6896 KB]

  Test 5: TEST OK [0.011 secs, 6896 KB]

  Test 6: TEST OK [0.022 secs, 6896 KB]

  Test 7: TEST OK [0.054 secs, 6896 KB]

  Test 8: TEST OK [0.065 secs, 6896 KB]

  Test 9: TEST OK [0.140 secs, 6896 KB]

  Test 10: TEST OK [0.896 secs, 6896 KB]

 

All tests OK.

 

Your program ('buylow') produced allcorrect answers! This is your

submission #15 for this problem.Congratulations!

 


0 0