0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 18:08:47 +00:00

#10183. 「一本通 5.5 练习 4」股票交易

https://loj.ac/s/1487923
This commit is contained in:
Baoshuo Ren 2022-06-21 19:51:04 +08:00
parent 5d2b144834
commit e3adcc1382
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
21 changed files with 123 additions and 0 deletions

63
LibreOJ/10183/10183.cpp Normal file
View File

@ -0,0 +1,63 @@
#include <iostream>
#include <cstring>
#include <deque>
#include <tuple>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 2005;
int n, m, w, f[N][N];
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
memset(f, 0xcf, sizeof(f));
cin >> n >> m >> w;
for (int i = 1; i <= n; i++) {
f[i][0] = 0;
int ap, bp, as, bs;
cin >> ap >> bp >> as >> bs;
// [2]: 从 0 张开始买入
for (int j = 0; j <= as; j++) {
f[i][j] = -j * ap;
}
// [1]: 不操作
for (int j = 0; j <= m; j++) {
f[i][j] = std::max(f[i][j], f[i - 1][j]);
}
if (i - w - 1 > 0) {
// [3]: 买入
std::deque<int> q1;
for (int j = 0; j <= m; j++) {
while (!q1.empty() && q1.front() < j - as) q1.pop_front();
while (!q1.empty() && f[i - w - 1][q1.back()] + q1.back() * ap <= f[i - w - 1][j] + j * ap) q1.pop_back();
q1.push_back(j);
f[i][j] = std::max(f[i][j], f[i - w - 1][q1.front()] - (j - q1.front()) * ap);
}
// [4]: 卖出
std::deque<int> q2;
for (int j = m; ~j; j--) {
while (!q2.empty() && q2.front() > j + bs) q2.pop_front();
while (!q2.empty() && f[i - w - 1][q2.back()] + q2.back() * bp <= f[i - w - 1][j] + j * bp) q2.pop_back();
q2.push_back(j);
f[i][j] = std::max(f[i][j], f[i - w - 1][q2.front()] + (q2.front() - j) * bp);
}
}
}
cout << f[n][0] << endl;
return 0;
}

BIN
LibreOJ/10183/data/trade0.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade0.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade5.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade6.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade7.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade8.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/10183/data/trade9.out (Stored with Git LFS) Normal file

Binary file not shown.