Buy low,Buy lower_usaco 4.3_dp

来源:互联网 发布:淘宝上什么叫匿名购买 编辑:程序博客网 时间:2024/05/21 10:15

Description


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.

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.

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.

Analysis


第一问不说了,经典dp,方程
f[i]=max(f[j])+1(num[j]>num[i])
第二问我们用d[i]表示以i结尾的最优方案数,那么显然d[i]=d[j](f[j]+1=f[i])
考虑到重复的价格结束算同种方案,那么记录nex[i]为与num[i]相同的下一个,每次只找最靠右的
ll会爆掉,要用高精度
玩了一发模板,挺好用

Code


/*ID:wjp13241PROG:buylowLANG:C++*/#include <string.h>#include <stdio.h>#include <math.h>#include <queue>#include <vector>#define dfo(i,a,b) for (int i=a;i>=b;i--)#define fo(i,a,b) for (int i=a;i<=b;i++)#define fil(x,t) memset(x,t,sizeof(x))#define max(x,y) x>y?x:y#define min(x,y) x<y?x:y#define INF 0x7f7f7f7f#define N 5021#define L 128using namespace std;struct bigNum{    int s[L];    inline bigNum operator +(bigNum b){        bigNum c;        fil(c.s,0);        int tmp=0;        dfo(i,L-1,1)        {            c.s[i]=(s[i]+b.s[i]+tmp)%10;            tmp=(s[i]+b.s[i]+tmp)/10;        }        return c;    }    inline void ouput(){        fo(i,1,L-1)            if (s[i])            {                fo(j,i,L-1)                    printf("%d",s[j]);                break;            }        printf("\n");    }};int f[N],t[N],nex[N];bigNum d[N];int main(){    freopen("buylow.in","r",stdin);    freopen("buylow.out","w",stdout);    int n;    scanf("%d",&n);    fo(i,1,n)    {        scanf("%d",&t[i]);        fil(d[i].s,0);    }    fo(i,1,n-1)        fo(j,i+1,n)            if (t[i]==t[j])            {                nex[i]=j;                break;            }    t[0]=INF;    d[0].s[L-1]=1;    nex[0]=0;    int ans=0;    fo(i,1,n)    {        fo(j,0,i-1)            if (t[j]>t[i])                f[i]=max(f[i],f[j]+1);        fo(j,0,i-1)            if (t[j]>t[i]&&f[j]+1==f[i]&&(!nex[j]||nex[j]>i))                d[i]=d[i]+d[j];        ans=max(ans,f[i]);    }    bigNum cnt;    fil(cnt.s,0);    fo(i,1,n)        if (f[i]==ans&&!nex[i])            cnt=cnt+d[i];    printf("%d ",ans);    cnt.ouput();    return 0;}
0 0