A9279 | Stock Trading
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
The first line contains two integers $n$ and $k$ , separated by a single space, with . The $i$ -th of the following $n$ lines contains a single integer $p_{i}$ ( $0<=p_{i}<=10^{12}$ ), where $p_{i}$ represents the price at which someone can either buy or sell one share of stock on day $i$ .
The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
- In subproblem F1 (8 points), $n$ will be between $1$ and $3000$ , inclusive.
- In subproblem F2 (15 points), $n$ will be between $1$ and $100000$ , inclusive.
- In subproblem F3 (10 points), $n$ will be between $1$ and $4000000$ , inclusive.
The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
- In subproblem F1 (8 points), $n$ will be between $1$ and $3000$ , inclusive.
- In subproblem F2 (15 points), $n$ will be between $1$ and $100000$ , inclusive.
- In subproblem F3 (10 points), $n$ will be between $1$ and $4000000$ , inclusive.
输入格式
For this problem, the program will only report the amount of the optimal profit, rather than a list of trades that can achieve this profit.
Therefore, the program should print one line containing a single integer, the maximum profit Manao can achieve over the next $n$ days with the constraints of starting with no shares on the first day of trading, always owning either zero or one shares of stock, and buying at most $k$ shares over the course of the $n$ -day trading period.
Therefore, the program should print one line containing a single integer, the maximum profit Manao can achieve over the next $n$ days with the constraints of starting with no shares on the first day of trading, always owning either zero or one shares of stock, and buying at most $k$ shares over the course of the $n$ -day trading period.
输出格式
In the first example, the best trade overall is to buy at a price of $1$ on day 9 and sell at a price of $9$ on day 10 and the second best trade overall is to buy at a price of $2$ on day 1 and sell at a price of $9$ on day 4. Since these two trades do not overlap, both can be made and the profit is the sum of the profits of the two trades. Thus the trade strategy looks like this:
```
<pre class="verbatim"><br></br>2 | 7 | 3 | 9 | 8 | 7 | 9 | 7 | 1 | 9<br></br>buy | | | sell | | | | | buy | sell<br></br>
```
The total profit is then $(9-2)+(9-1)=15$ .
In the second example, even though Manao is allowed up to 5 trades there are only 4 profitable trades available. Making a fifth trade would cost Manao money so he only makes the following 4:
```
<pre class="verbatim"><br></br>2 | 7 | 3 | 9 | 8 | 7 | 9 | 7 | 1 | 9<br></br>buy | sell | buy | sell | | buy | sell | | buy | sell<br></br>
```
The total profit is then $(7-2)+(9-3)+(9-7)+(9-1)=21$ .
```
<pre class="verbatim"><br></br>2 | 7 | 3 | 9 | 8 | 7 | 9 | 7 | 1 | 9<br></br>buy | | | sell | | | | | buy | sell<br></br>
```
The total profit is then $(9-2)+(9-1)=15$ .
In the second example, even though Manao is allowed up to 5 trades there are only 4 profitable trades available. Making a fifth trade would cost Manao money so he only makes the following 4:
```
<pre class="verbatim"><br></br>2 | 7 | 3 | 9 | 8 | 7 | 9 | 7 | 1 | 9<br></br>buy | sell | buy | sell | | buy | sell | | buy | sell<br></br>
```
The total profit is then $(7-2)+(9-3)+(9-7)+(9-1)=21$ .
输入输出样例
输入 #1
10 2 2 7 3 9 8 7 9 7 1 9
输出 #1
15
输入 #2
10 5 2 7 3 9 8 7 9 7 1 9
输出 #2
21
In the first example, the best trade overall is to buy at a price of $1$ on day 9 and sell at a price of $9$ on day 10 and the second best trade overall is to buy at a price of $2$ on day 1 and sell at a price of $9$ on day 4. Since these two trades do not overlap, both can be made and the profit is the sum of the profits of the two trades. Thus the trade strategy looks like this:
```
<pre class="verbatim"><br></br>2 | 7 | 3 | 9 | 8 | 7 | 9 | 7 | 1 | 9<br></br>buy | | | sell | | | | | buy | sell<br></br>
```
The total profit is then $(9-2)+(9-1)=15$ .
In the second example, even though Manao is allowed up to 5 trades there are only 4 profitable trades available. Making a fifth trade would cost Manao money so he only makes the following 4:
```
<pre class="verbatim"><br></br>2 | 7 | 3 | 9 | 8 | 7 | 9 | 7 | 1 | 9<br></br>buy | sell | buy | sell | | buy | sell | | buy | sell<br></br>
```
The total profit is then $(7-2)+(9-3)+(9-7)+(9-1)=21$ .
```
<pre class="verbatim"><br></br>2 | 7 | 3 | 9 | 8 | 7 | 9 | 7 | 1 | 9<br></br>buy | | | sell | | | | | buy | sell<br></br>
```
The total profit is then $(9-2)+(9-1)=15$ .
In the second example, even though Manao is allowed up to 5 trades there are only 4 profitable trades available. Making a fifth trade would cost Manao money so he only makes the following 4:
```
<pre class="verbatim"><br></br>2 | 7 | 3 | 9 | 8 | 7 | 9 | 7 | 1 | 9<br></br>buy | sell | buy | sell | | buy | sell | | buy | sell<br></br>
```
The total profit is then $(7-2)+(9-3)+(9-7)+(9-1)=21$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted