zoukankan      html  css  js  c++  java
  • bzoj 1578 [Usaco2009 Feb]Stock Market 股票市场 背包dp

    1578: [Usaco2009 Feb]Stock Market 股票市场

    Time Limit: 10 Sec  Memory Limit: 64 MB
    Submit: 613  Solved: 349
    [Submit][Status][Discuss]

    Description

    尽管奶牛们天生谨慎,她们仍然在住房抵押信贷市场中受到打击,现在她们开始着手于股市。 Bessie很有先见之明,她不仅知道今天S (2 <= S <= 50)只股票的价格,还知道接下来一共D(2 <= D <= 10)天的(包括今天)。 给定一个D天的股票价格矩阵(1 <= 价格 <= 1000)以及初始资金M(1 <= M <= 200,000),求一个最优买卖策略使得最大化总获利。每次必须购买股票价格的整数倍,同时你不需要花光所有的钱(甚至可以不花)。这里约定你的获利不可能超过500,000。 考虑这个牛市的例子(这是Bessie最喜欢的)。在这个例子中,有S=2只股票和D=3天。奶牛有10的钱来投资。 今天的价格 | 明天的价格 | | 后天的价格 股票 | | | 1 10 15 15 2 13 11 20   以如下策略可以获得最大利润,第一天买入第一只股票。第二天把它卖掉并且迅速买入第二只,此时还剩下4的钱。最后一天卖掉第二只股票,此时一共有4+20=24的钱。

    Input

    * 第一行: 三个空格隔开的整数:S, D, M

    * 第2..S+1行: 行s+1包含了第s只股票第1..D天的价格

    Output

    * 第一行: 最后一天卖掉股票之后最多可能的钱数。

    Sample Input

    2 3 10
    10 15 15
    13 11 20

    Sample Output

    24

    HINT

     

    Source

    Gold

    题解:

       完全背包

     1 #include<cstring>
     2 #include<cmath>
     3 #include<cstdio>
     4 #include<algorithm>
     5 #include<iostream>
     6 
     7 #define S 51
     8 #define D 11
     9 #define M 500010
    10 
    11 #define Wb putchar(' ')
    12 #define We putchar('
    ')
    13 using namespace std;
    14 inline int read()
    15 {
    16     int x=0,f=1;char ch=getchar();
    17     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    18     while(isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    19     return x*f;
    20 }
    21 inline void write(int x)
    22 {
    23     if(x<0) putchar('-'),x=-x;
    24     if (x==0) putchar(48);
    25     int num=0;char c[15];
    26     while(x) c[++num]=(x%10)+48,x/=10;
    27     while(num) putchar(c[num--]);
    28 }
    29 
    30 int p[D][S], best[D], dp[M];
    31 template<class T>T _max(T a,T b)
    32 {
    33     return (a>b)?a:b;
    34 }
    35 
    36 int tmp[S], top;
    37 int s,d,m;
    38 
    39 
    40 int main() 
    41 {
    42     s=read(),d=read(),m=read();
    43     
    44     for(int i=1;i<=s;i++)
    45         for(int j=1;j<=d;j++)
    46             p[j][i]=read();
    47     
    48     best[1]=m;
    49     for(int i=2;i<=d;i++) 
    50     {
    51         top=0;
    52         for(int j=1;j<=s;j++)
    53             if (p[i][j]>p[i-1][j])
    54                 tmp[++top]=j;
    55         for(int k=1;k<=best[i-1];k++) dp[k]=k;
    56         for(int j=1;j<=top;j++)
    57             for(int k=p[i-1][tmp[j]];k<=best[i-1];k++)
    58                 dp[k]=_max(dp[k],dp[k-p[i-1][tmp[j]]]+p[i][tmp[j]]);
    59         best[i]= _max(best[i],dp[best[i-1]]);
    60     }
    61     write(best[d]);
    62 }
     
  • 相关阅读:
    Web APIs——DOM
    案例:动态生成表格
    案例:简单版发布留言功能
    案例:下拉菜单功能
    案例:tab栏切换功能(原生JS写法)
    时间复杂度
    ubuntu下安装LNMP
    btree索引和hash索引的区别
    StandardServer.await: Invalid command 'GET / HTTP/1.1' received
    MySQL----触发器
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/8983591.html
Copyright © 2011-2022 走看看