F - Earn to Advance
F - Earn to Advance
Problem Statement
There is a grid with rows and columns. Let denote the square at the -th row from the top and -th column from the left.
Takahashi is initially at square with zero money.
When Takahashi is at square , he can perform one of the following in one action:
- Stay at the same square and increase his money by .
- Pay from his money and move to square .
- Pay from his money and move to square .
He cannot make a move that would make his money negative or take him outside the grid.
If Takahashi acts optimally, how many actions does he need to reach square ?
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
3
1 2 3
3 1 2
2 1 1
1 2
4 3
4 2
1 5 7
5 3 3
Sample Output 1
8
It is possible to reach square in eight actions as follows:
- Stay at square and increase money by . His money is now .
- Pay money and move to square . His money is now .
- Stay at square and increase money by . His money is now .
- Stay at square and increase money by . His money is now .
- Stay at square and increase money by . His money is now .
- Pay money and move to square . His money is now .
- Pay money and move to square . His money is now .
- Pay money and move to square . His money is now .
Sample Input 2
3
1 1 1
1 1 1
1 1 1
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000
1000000000 1000000000 1000000000
1000000000 1000000000 1000000000
Sample Output 2
4000000004
解题思路
要求 的最小操作次数,不妨先考虑最后一次使用第一种操作的位置,假设为 。为了保证 不存在金额为负数的情况,需要在 处停留一定的次数。假设 表示 的所有路径中只执行第二、三种操作的最小金额, 表示 还剩的金额,那么在 处至少要停留 次。因此当 是最后一次使用第一种操作的位置时, 的最小操作次数就是 。
现在问题就变成了求 的最小操作次数,显然可以沿用上面的思路。为此可以考虑 dp,定义 表示 的最小操作次数,根据最后一次使用第一种操作的位置 进行状态划分,状态转移方程就是 ,其中 。
考虑维护 的最大值,当 时,直接令 。否则如果 ,。
可以用 dp 预处理出来,状态转移方程就是 。
AC 代码如下,时间复杂度为 :
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 85;
int p[N][N], r[N][N], d[N][N];
LL f[N][N], g[N][N], h[N][N][N][N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &p[i][j]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j < n; j++) {
scanf("%d", &r[i][j]);
}
}
for (int i = 1; i < n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &d[i][j]);
}
}
memset(h, 0x3f, sizeof(h));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
h[i][j][i][j] = 0;
for (int u = i; u <= n; u++) {
for (int v = j; v <= n; v++) {
if (u == i && v == j) continue;
h[i][j][u][v] = min(h[i][j][u - 1][v] + d[u - 1][v], h[i][j][u][v - 1] + r[u][v - 1]);
}
}
}
}
memset(f, 0x3f, sizeof(f));
f[1][1] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int u = 1; u <= i; u++) {
for (int v = 1; v <= j; v++) {
LL c = (max(0ll, h[u][v][i][j] - g[u][v]) + p[u][v] - 1) / p[u][v];
if (f[i][j] > f[u][v] + c + i - u + j - v) {
f[i][j] = f[u][v] + c + i - u + j - v;
g[i][j] = g[u][v] + p[u][v] * c - h[u][v][i][j];
}
else if (f[i][j] == f[u][v] + c + i - u + j - v) {
g[i][j] = max(g[i][j], g[u][v] + p[u][v] * c - h[u][v][i][j]);
}
}
}
}
}
printf("%lld", f[n][n]);
return 0;
}
上面代码所需的内存是 左右。还可以把 优化到两维。在枚举 的时候定义 表示 只执行第二、三种操作的最小金额。状态转移方程就是 。
AC 代码如下,时间复杂度为 :
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 85;
int p[N][N], r[N][N], d[N][N];
LL f[N][N], g[N][N], h[N][N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &p[i][j]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j < n; j++) {
scanf("%d", &r[i][j]);
}
}
for (int i = 1; i < n; i++) {
for (int j = 1; j <= n; j++) {
scanf("%d", &d[i][j]);
}
}
memset(f, 0x3f, sizeof(f));
f[1][1] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
memset(h, 0x3f, sizeof(h));
h[i][j] = 0;
for (int u = i; u; u--) {
for (int v = j; v; v--) {
if (u == i && v == j) continue;
h[u][v] = min(h[u + 1][v] + d[u][v], h[u][v + 1] + r[u][v]);
}
}
for (int u = 1; u <= i; u++) {
for (int v = 1; v <= j; v++) {
LL c = (max(0ll, h[u][v] - g[u][v]) + p[u][v] - 1) / p[u][v];
if (f[i][j] > f[u][v] + c + i - u + j - v) {
f[i][j] = f[u][v] + c + i - u + j - v;
g[i][j] = g[u][v] + p[u][v] * c - h[u][v];
}
else if (f[i][j] == f[u][v] + c + i - u + j - v) {
g[i][j] = max(g[i][j], g[u][v] + p[u][v] * c - h[u][v]);
}
}
}
}
}
printf("%lld", f[n][n]);
return 0;
}
参考资料
Editorial - Toyota Programming Contest 2024#3(AtCoder Beginner Contest 344):https://atcoder.jp/contests/abc344/editorial/9494
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/18064351
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效