题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401

题意:炒股。第i天买入一股的价钱api,卖出一股的价钱bpi,最多买入asi股,最多卖出bsi股。两次操作(买入或卖出)中间必须相差W天。炒股时间为n。任意时间手中的股票不大于MaxP。求最大收益。

dp[i][j]代表第i天手上有j股的最大收益,dp[i][j]=max(dp[i-1][j],dp[i-W][k]+(j-k)*ap[i],dp[i-W][k]+(k-j)*bp[i]);

dp[i-W][k]+j*ap[i]-k*ap[i];

dp[i-W][k]+k*bp[i]-j*bp[i];

用单调队列维护

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<string>
  6. #include<algorithm>
  7. #define inf 0x7fffffff
  8. struct op{
  9. int x,p;
  10. }q[],temp;
  11. int n,m,w;
  12. int ap[],bp[],as[],bs[];
  13. int dp[][],h,t;
  14. int main(){
  15. int Tcase;
  16. scanf("%d",&Tcase);
  17. while (Tcase--){
  18. scanf("%d%d%d",&n,&m,&w);
  19. for (int i=;i<=n;i++)
  20. scanf("%d%d%d%d",&ap[i],&bp[i],&as[i],&bs[i]);
  21. for (int i=;i<=n;i++)
  22. for (int j=;j<=m;j++)
  23. dp[i][j]=-inf;
  24. for (int i=;i<=w+;i++)
  25. for (int j=;j<=as[i];j++)
  26. dp[i][j]=-ap[i]*j;
  27. for (int i=;i<=n;i++){
  28. for (int j=;j<=m;j++)
  29. dp[i][j]=std::max(dp[i][j],dp[i-][j]);
  30. if (i<=w+) continue;
  31. h=t=;
  32. for (int j=;j<=m;j++){
  33. temp.p=j;
  34. temp.x=dp[i-w-][j]+ap[i]*j;
  35. for (;h<t&&q[t-].x<temp.x;t--);
  36. q[t++]=temp;
  37. for (;h<t&&q[h].p+as[i]<j;h++);
  38. dp[i][j]=std::max(dp[i][j],q[h].x-ap[i]*j);
  39. }
  40. h=t=;
  41. for (int j=m;j>=;j--){
  42. temp.p=j;
  43. temp.x=dp[i-w-][j]+bp[i]*j;
  44. for (;h<t&&q[t-].x<temp.x;t--);
  45. q[t++]=temp;
  46. for (;h<t&&q[h].p-bs[i]>j;h++);
  47. dp[i][j]=std::max(dp[i][j],q[h].x-bp[i]*j);
  48. }
  49. }
  50. int ans=;
  51. for (int i=;i<=m;i++)
  52. ans=std::max(ans,dp[n][i]);
  53. printf("%d\n",ans);
  54. }
  55. }

HDU 3401 Trade(单调队列优化)的更多相关文章

  1. HDU 3401 Trade(斜率优化dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:有一个股市,现在有T天让你炒股,在第i天,买进股票的价格为APi,卖出股票的价格为BPi,同时最多买 ...

  2. HDU-3401 Trade 单调队列优化DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 DP方程容易想出来,f[i][j]表示第i天拥有j个股票的最优解,则: 1.不买不卖,f[i][ ...

  3. hdu3401 Trade 单调队列优化dp

    Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  4. HDU 3507 PrintArticle (单调队列优化)

    题意:给出一个数列C,一个数字M,将数列分成若干段,每段的代价为(设这段的数字为k个): dp[i]=min(dp[j]+(sum[i]-sum[j])*(sum[i]-sum[j])+M) 若j1& ...

  5. 【单调队列优化dp】HDU 3401 Trade

    http://acm.hdu.edu.cn/showproblem.php?pid=3401 [题意] 知道之后n天的股票买卖价格(api,bpi),以及每天股票买卖数量上限(asi,bsi),问他最 ...

  6. bzoj1855: [Scoi2010]股票交易 单调队列优化dp ||HDU 3401

    这道题就是典型的单调队列优化dp了 很明显状态转移的方式有三种 1.前一天不买不卖: dp[i][j]=max(dp[i-1][j],dp[i][j]) 2.前i-W-1天买进一些股: dp[i][j ...

  7. 单调队列优化DP,多重背包

    单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...

  8. HDU 3401 Trade

    Trade Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 3401 ...

  9. 单调队列优化DP——习题收集

    前言 感觉可以用单调队列优化dp的模型还是挺活的,开个随笔记录一些遇到的比较有代表性的模型,断续更新.主要做一个收集整理总结工作. 记录 0x01 POJ - 1821 Fence,比较适合入门的题, ...

随机推荐

  1. BZOJ 2732 射箭

    http://www.lydsy.com/JudgeOnline/problem.php?id=2732 题意:给你n个靶子,让你求是否有一个经过原点的抛物线经过最多的前k个靶子,求出最大的k 思路: ...

  2. LeetCode_3 sum

    Given an array S of n integers, are there elements a, b, c ? Find all unique triplets in the array w ...

  3. CCI_chapter 4 trees and Grapths

    4.1Implement a function to check if a tree is balanced For the purposes of this question,a balanced ...

  4. Cracking the coding interview--Q1.1

    原文: Implement an algorithm to determine if a string has all unique characters. What if you can not u ...

  5. NSIS操作系统环境变量

    手头有个项目需要修改PATH变量 需要!include "EnvVarUpdate.nsh" 以下是NSIS脚本代码 ; Script generated by the HM NI ...

  6. hdu1028:整数拆分

    求整数的拆分数.. 一种解法是母函数 #include <iostream> #include <stdio.h> #include<string.h> #incl ...

  7. window.open和window.close的使用详解

    有时候,我们想通过JS实现一个<a>的新开标签的效果,此时我们想到了window.open方法实现.那么window.open到底应该怎么使用呢?   我们知道window.open可以新 ...

  8. jquery href属性和click事件冲突

    a标签的定义如下: <a href="javascript:void(0);">test</a> jquery中的click事件: $("a&qu ...

  9. 获取某个文件夹中所有txt文件

    <?php // 获取文件夹中的所有txt文件名 $dir = "D:/a"; //这里输入其他路径 $handle = opendir($dir."." ...

  10. CentOS系统、Jdk、Tomcat安装实战

    CentOS系统.Jdk.Tomcat安装实战                                                     第一次接触Liunx系统,都说J2EE系统在Li ...