Buy low,Buy lower_usaco 4.3_dp

123 篇文章 0 订阅
52 篇文章 0 订阅

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:wjp13241
PROG:buylow
LANG: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 128
using 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值