洛谷传送门php
JDOJ传送门app
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).ide
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.ui
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.this
|Stock|Today's price| Tomorrow's price| | Two days hence Stock | | | :-----------: | :-----------: | :-----------: | :-----------: | | AA | 10 | 15 | 15 | |BB | 13| 11|20 |spa
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_sdcode
* Line 1: The maximum amount of money possible to have after selling on day D.ip
尽管奶牛天生谨慎,它们仍然在住房抵押信贷市场中大受打击,如今它们准备在股市上碰碰运气。贝西有内部消息,她知道 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≤j≤D,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; }