USACO Stock Market

时间:2023-02-26 13:46:24

洛谷 P2938 [USACO09FEB]股票市场Stock Market

洛谷传送门

JDOJ 2625: USACO 2009 Feb Gold 2.Stock Market

JDOJ传送门

题目描述

Despite their innate prudence, the cows took a beating in the home mortgage market and now are trying their hand at stocks. Happily, Bessie is prescient and knows not only today's S (2 <= S <= 50) stock prices but also the future stock prices for a total of D days (2 <= D <= 10).

Given the matrix of current and future stock prices on various days (1 <= PR_sd <= 1,000) and an initial M (1 <= M <= 200,000) units of money, determine an optimal buying and selling strategy in order to maximize the gain realized by selling stock on the final day. Shares must be purchased in integer multiples, and you need not spend all the money (or any money). It is guaranteed that you will not be able to earn a profit of more than 500,000 units of money.

Consider the example below of a bull (i.e., improving) market, the kind Bessie likes most. In this case, S=2 stocks and D=3 days. The cows have 10 units of money to invest.

|Stock|Today's price| Tomorrow's price| | Two days hence Stock | | | :-----------: | :-----------: | :-----------: | :-----------: | | AA | 10 | 15 | 15 | |BB | 13| 11|20 |

If money is to be made, the cows must purchase stock 1 on day 1. Selling stock 1 on day 2 and quickly buying stock 2 yields 4 money in the bank and one share of 2. Selling stock 2 on the final day brings in 20 money for a total of 24 money when the 20 is added to the bank.

输入格式

* Line 1: Three space-separated integers: S, D, and M

* Lines 2..S+1: Line s+1 contains the D prices for stock s on days 1..D: PR_sd

输出格式

* Line 1: The maximum amount of money possible to have after selling on day D.

题意翻译

题目描述

尽管奶牛天生谨慎,它们仍然在住房抵押信贷市场中大受打击,现在它们准备在股市上碰碰运气。贝西有内部消息,她知道 SS 只股票在今后 DD 天内的价格。

假设在一开始,她筹集了 MM 元钱,那么她该怎样操作才能赚到最多的钱呢?贝西在每天可以买卖多只股票,也可以多次买卖同一只股票,交易单位必须是整数,数量不限。举一个牛市的例子:

假设贝西有 10 元本金,股票价格如下:

股票 今天的价格 明天的价格 后天的价格
AA 10 15 15
BB 13 11 20

最赚钱的做法是:今天买入 AA 股 1 张,到明天把它卖掉并且买入 B 股 1 张,在后天卖掉 B股,这样贝西就有 24 元了。

输入格式

第一行:三个整数 S, D 和 M,2 ≤ S ≤ 502≤S≤50 ; 2 ≤ D ≤ 102≤D≤10 ; 1 ≤ M ≤ 2000001≤M≤200000

第二行到第 S + 1 行:第 i + 1 行有 D 个整数: P_{i;1}P**i;1 到 P_{i;D}P**i;D,表示第 ii 种股票在第一天到最后一天的售价,对所有1 ≤ j ≤ D1≤jD,1 ≤ Pi1≤P**i;j ≤ 1000j≤1000

输出格式

单个整数:表示奶牛可以获得的最大钱数,保证这个数不会超过 500000500000

输入输出样例

输入 #1复制

输出 #1复制

题解:

一道怪异的背包问题。

首先,我们能明确一点,就是DP的决策:

在第\(i\)天,有这么几种决策方式:

第一种:不买。

第二种:买完第二天卖。

第三种:买完在手中存几天后再卖。

但是第三种决策完全可以转化成第二种决策,原理是这样的:

对于一只股票,我们在第\(i\)天买第\(j\)天卖,其效果可以被看为在第\(i\)天买,第\(k\)天卖(\(i\le k\le j\)),当天再买回来,第\(j\)天卖。

这样的话,我们的第三种决策就可以变成:买完第二天卖,第二天再买回来。这就解决了DP的无后效性的问题。我们就可以开始设计DP过程了。

根据上面的分析,我们发现买股票变成了相邻两天的事情。那么,每一天对于每种物品只有两种选择:买还是不买。

诶?好像完全背包欸!

那么,对于每一天,我们都做一次完全背包:这个背包的体积就是当前的资金,每个物品的体积是当天的价值,价值为当天的价值减去前一天的价值。

所以我们可以跑\(d-1\)次完全背包,选出答案最大的一次。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int s,d,m,maxx;
int map[60][20],dp[500001];
int main()
{
scanf("%d%d%d",&s,&d,&m);
for(int i=1;i<=s;i++)
for(int j=1;j<=d;j++)
scanf("%d",&map[i][j]);
for(int i=2;i<=d;i++)
{
maxx=-1;
memset(dp,0,sizeof(dp));
for(int j=1;j<=s;j++)
for(int k=map[j][i-1];k<=m;k++)
{
dp[k]=max(dp[k],dp[k-map[j][i-1]]+map[j][i]-map[j][i-1]);
maxx=max(maxx,dp[k]);
}
m+=maxx;
}
printf("%d",m);
return 0;
}

USACO Stock Market的更多相关文章

  1. USACO 2009 Feb 股票市场 Stock Market

    USACO 2009 Feb 股票市场 Stock Market Description 尽管奶牛们天生谨慎,她们仍然在住房抵押信贷市场中大受打击,现在她们准备在股市 上碰碰运气.贝西开挂了,她知道S ...

  2. BZOJ 1578&colon; &lbrack;Usaco2009 Feb&rsqb;Stock Market 股票市场&lpar; 背包dp &rpar;

    我们假设每天买完第二天就卖掉( 不卖出也可以看作是卖出后再买入 ), 这样就是变成了一个完全背包问题了, 股票价格为体积, 第二天的股票价格 - 今天股票价格为价值.... 然后就一天一天dp... ...

  3. 1578&colon; &lbrack;Usaco2009 Feb&rsqb;Stock Market 股票市场

    1578: [Usaco2009 Feb]Stock Market 股票市场 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 414  Solved: 1 ...

  4. An Introduction to Stock Market Data Analysis with R &lpar;Part 1&rpar;

    Around September of 2016 I wrote two articles on using Python for accessing, visualizing, and evalua ...

  5. &lpar;转&rpar; Using the latest advancements in AI to predict stock market movements

    Using the latest advancements in AI to predict stock market movements 2019-01-13 21:31:18 This blog ...

  6. &lbrack;bzoj1578&rsqb;&lbrack;Usaco2009 Feb&rsqb;Stock Market 股票市场&lowbar;完全背包dp

    Stock Market 股票市场 bzoj-1578 Usaco-2009 Feb 题目大意:给定一个$S\times D$的大矩阵$T$,其中$T[i][j]$表示第i支股票第j天的价格.给定初始 ...

  7. word Stock Market Indices

    Stock Market Indices USA Africa Asia and Pacific Canada Europe Middle East South America Internation ...

  8. 股票市场问题(The Stock Market Problem)

    Question: Let us suppose we have an array whose ith element gives the price of a share on the day i. ...

  9. 【BZOJ】1578&colon; &lbrack;Usaco2009 Feb&rsqb;Stock Market 股票市场

    [题意]给定s个股票和d天,给出价格矩阵s*d,每天可以买入或卖出整数倍股票,初始资金m,求最大利益.m<=200000,s<=50,d<=10. [算法]完全背包 [题解]关键在于 ...

随机推荐

  1. JavaScript学习笔记-实例详解-类(一)

    实例详解-类(一): //每个javascript函数(除了bind())都自动拥有一个prototype对象// 在未添加属性或重写prototype对象之前,它只包含唯一一个不可枚举属性const ...

  2. 【1】第一次电话面试---上海EMC

    时间是2016//11月,投的是上海的EMC2公司的JavaWeb开发岗,第一次接到的电话面试,问的题目很基础基础,很遗憾,本人在掌握的太不好,回答的很乱,目测定挂.下面记下HR问的问题及回答. 首先 ...

  3. 一步一步使用sklearn

    http://kukuruku.co/hub/python/introduction-to-machine-learning-with-python-andscikit-learn Hello, %u ...

  4. oracle时间模型

    Oracle在9i或者早期的版本,对于性能优化方面,主要采用命中率模型,后面的版本,也保留着命中率模型,比如在awr报告中,Instance Efficiency Percentages (Targe ...

  5. 计算机编码方式详解(Unicode、UTF-8、UTF-16、ASCII)

    整理这篇文章的动机是两个问题: 问题一: 使用Windows记事本的"另存为",可以在GBK.Unicode.Unicode big endian和UTF-8这几种编码方式间相互转 ...

  6. EF:分页查询 &plus; 条件查询 &plus; 排序

    /// <summary> /// linq扩展类---zxh /// </summary> /// <typeparam name="T">& ...

  7. iPhone照片格式heic图片怎么打开

    苹果自iOS11系统之后默认的是heic图片格式,在电脑和安卓中都无法直接查看,需要将其转换图片格式,那苹果heic图片怎么转换成jpg格式?下面我们一起来看看吧! 使用工具:电脑.图片 操作方法: ...

  8. PL&sol;SQL设置

    PL/SQL 自定义快捷键(比如输入s,直接就显示select * from) 1.1 修改Code assistant快捷键tools->preferences->User Interf ...

  9. mysql中InnoDB表为什么要建议用自增列做主键

    InnoDB引擎表的特点 1.InnoDB引擎表是基于B+树的索引组织表(IOT) 关于B+树 (图片来源于网上) B+ 树的特点: (1)所有关键字都出现在叶子结点的链表中(稠密索引),且链表中的关 ...

  10. Sql语法高级应用之一:使用sql语句如何实现不同的角色看到不同的数据

    前言 在常见的管理系统中,通常都有这样的需求,管理员可以看到所有数据,部门可以看到本部门的数据,组长可以看到自己组的数据,组员只能看到自己相关的数据. 一般人的做法是,根据不同的角色通过if...el ...