zoukankan      html  css  js  c++  java
  • hdu 3401 单调队列优化+dp

    http://acm.hdu.edu.cn/showproblem.php?pid=3401

    Trade

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 5188    Accepted Submission(s): 1776


    Problem Description
    Recently, lxhgww is addicted to stock, he finds some regular patterns after a few days' study.
    He forecasts the next T days' stock market. On the i'th day, you can buy one stock with the price APi or sell one stock to get BPi.
    There are some other limits, one can buy at most ASi stocks on the i'th day and at most sell BSi stocks.
    Two trading days should have a interval of more than W days. That is to say, suppose you traded (any buy or sell stocks is regarded as a trade)on the i'th day, the next trading day must be on the (i+W+1)th day or later.
    What's more, one can own no more than MaxP stocks at any time.

    Before the first day, lxhgww already has infinitely money but no stocks, of course he wants to earn as much money as possible from the stock market. So the question comes, how much at most can he earn?
     
    Input
    The first line is an integer t, the case number.
    The first line of each case are three integers T , MaxP , W .
    (0 <= W < T <= 2000, 1 <= MaxP <= 2000) .
    The next T lines each has four integers APi,BPi,ASi,BSi( 1<=BPi<=APi<=1000,1<=ASi,BSi<=MaxP), which are mentioned above.
     
    Output
    The most money lxhgww can earn.
     
    Sample Input
    1 5 2 0 2 1 1 1 2 1 1 1 3 2 1 1 4 3 1 1 5 4 1 1
     
    Sample Output
    3
      感觉初始化有点坑,,,,
    由于间隔w天才能进行第二次购买,对于第i天想进行交易的股票来说之前的w天如果可以转移说明之前没进行过交易,所以这几天的数值都一样,所以不必重复的更新,
    只从前i-w-1这一天转移就好了。
    so  f[i][j]=MAX{f[i][j-1], f[i-w-1][k]-(j-k)*ap[i] k<=j&&j-k<=as[i], f[i-w-1][k]+(k-j)*bp[i] | k>=j&&k-j<=bs[i] };
    显然可以用单调队列加速,但是注意要提前处理前w天所有购买后的花费, 因为对于i-w-1小于零的天我们直接忽略,导致并没有计算他们,所以切记初始化!
    不必考虑前w+1天卖,因为如果想卖说明前面买了,但是两天都处于前w+1天中间隔肯定小于w天,因此证明得前w+1天里不可能发生卖的操作!
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<queue>
     5 #include<algorithm>
     6 using namespace std;
     7 #define inf 120
     8 #define LL long long
     9 inline int read(int f = 1)
    10 { 
    11     char c = getchar();while (!isdigit(c)) { if (c == '-')f = -1; c = getchar(); }
    12     int r = 0; while (isdigit(c)) { r = r * 10 + c - '0';c = getchar(); } return r*f;
    13 }
    14 int f[2005][2005];
    15 int ap[2005], bp[2005], as[2005], bs[2005];
    16 struct node2 { int w, k; };
    17 deque<node2>q;
    18 int main()
    19 {
    20     int i, j, k, cas, T, MAX_P, W;
    21     cin >> cas;
    22     while (cas--) {
    23         T = read();
    24         MAX_P = read();
    25         W = read();
    26         for (i = 1;i <= T;++i)
    27         {
    28             ap[i] = read();
    29             bp[i] = read();
    30             as[i] = read();
    31             bs[i] = read();
    32         }
    33         memset(f, -inf, sizeof(f));
    34         for (int i = 1; i <= W + 1; i++) {//第一天到W+1天只都是只能买的
    35             for (int j = 0; j <= min(MAX_P, as[i]); j++) {
    36                 f[i][j] = -ap[i] * j;
    37             }
    38         }
    39         f[0][0] = 0;
    40         for (i = 1;i <= T;++i)
    41         {
    42             int u = i - W - 1;
    43             for (j = 0;j <= MAX_P;++j)
    44             {
    45                 f[i][j] = max(f[i][j],f[i - 1][j]);
    46                 if (u < 0)continue;
    47                 while (!q.empty() && q.back().w < f[u][j] + j*ap[i])q.pop_back();
    48                 q.push_back(node2{f[u][j]+j*ap[i],j});
    49                 while (!q.empty() && j - q.front().k > as[i])q.pop_front();
    50                 if (!q.empty()) f[i][j] = max(f[i][j], q.front().w - j*ap[i]);
    51             }
    52             if (u < 0)continue;
    53             q.clear();
    54             for (j = MAX_P;j >= 0;--j)
    55             {
    56                 while (!q.empty() && q.back().w < f[u][j] + j*bp[i])q.pop_back();
    57                 q.push_back(node2{f[u][j]+j*bp[i],j});
    58                 while (!q.empty() &&  q.front().k -j> bs[i])q.pop_front();
    59                 if (!q.empty()) f[i][j] = max(f[i][j], q.front().w - j*bp[i]);
    60             }
    61             q.clear();
    62         }
    63         int ans = 0;
    64         for (i = 0;i <= MAX_P;++i)
    65             ans = max(ans,f[T][i]);
    66         printf("%d
    ", ans);
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    跟初学者学习IbatisNet第三篇
    跟初学者学习IbatisNet第二篇
    跟初学者学习IbatisNet第一篇
    读书时如何做读书笔记
    CCNET+MSBuild+SVN实现每日构建
    WebApi 通过类名获取类并实例化
    HttpClient设置代理
    dotnet core命令行启动如何支持如何找到进程
    redis 在Windows下的安装及基本操作(更新中~~~)
    Spring实现IoC的多种方式
  • 原文地址:https://www.cnblogs.com/zzqc/p/7424383.html
Copyright © 2011-2022 走看看