当前位置: 首页 > news >正文

Grid-dp,交互

P14000 Grid-dp,交互

P14000 Grid

题意

给定一个 \(N*M\) 的矩阵,从 0,0 开始,每次只能向一个方向移动任意格,每次移动会得到 (令 \(x\) 为当前值, \(y\) 为转移) \(\left| a_x - a_y\right| -c\) , 所以 $ dp_x = dp_y +\left|a_x - a_y\right| -c$,如果当前值更大,我们希望 \(dp_y-a_y\) 最小,同理,如果当前值更小,我们希望 \(dp_y+a_y\) 最大。

code + greader
// code
#ifndef ONLINE_JUDGE
#include "grid.h"
#endif // ONLINE_JUDGE#include <bits/stdc++.h>
#define ll long long
using namespace std;constexpr ll INF = 0x3f3f3f3f3f3f3f3f;// 令x为当前值,y为转移
// dp_x = dp_y + |a_x - a_y| -c
// 如果当前值更大,我们希望 dp_y-a_y 最小,此时+x
// 同理,如果当前值更小,我们希望dp_y+a_y最大,此时-x
// 令ma1为dp_y+a_ylong long max_profit(int n, int m, int c, std::vector<std::vector<int>> a)
{vector<vector<ll>> dp(n,vector<ll>(m,-INF));vector<ll> ma1_r(n,-INF) ,ma2_r(n,-INF);vector<ll> ma1_c(m,-INF) ,ma2_c(m,-INF);dp[0][0]=0;ma1_r[0]=a[0][0];  // 第0行 + 的最大值ma2_r[0]=-a[0][0]; // - 的最大值ma1_c[0]=a[0][0];ma2_c[0]=-a[0][0];for(int i=0;i<n;++i){for(int j=0;j<m;++j){if(!i && !j) continue;int x=a[i][j];ll rm=max(ma1_r[i]-x,ma2_r[i]+x)-c;ll cm=max(ma1_c[j]-x,ma2_c[j]+x)-c;dp[i][j]=max(rm,cm);ma1_r[i]=max(ma1_r[i],dp[i][j]+x);ma2_r[i]=max(ma2_r[i],dp[i][j]-x);ma1_c[j]=max(ma1_c[j],dp[i][j]+x);ma2_c[j]=max(ma2_c[j],dp[i][j]-x);}}return dp[n-1][m-1];
}
code + greader
// greader
#include "grid.h"
#include<iostream>
#include<vector>
using namespace std;
int main ()
{freopen("sample.in","r",stdin);// 本地测试freopen("sample.out","w",stdout);int n, m, c;cin >> n >> m >> c;vector<vector<int>> inp(n, vector<int>(m));for(int i = 0; i < n; i ++){for(int j = 0; j < m; j ++){cin >> inp[i][j];}}long long ans = max_profit(n, m, c, inp);cout << ans << endl;return 0;
}
//grid.h
#include <vector>long long max_profit(int N, int M, int C, std::vector<std::vector<int>> A);
http://www.rkmt.cn/news/57319.html

相关文章:

  • 2025 年国内电容源头厂家最新推荐排行榜:聚焦核心技术与品质,五大实力品牌选购指南电解电容/薄膜电容公司推荐
  • 初一上册CSP-J和期中考试反思
  • modbus(二)用NModbus4库实现Modbus tcp从站
  • 计算机字长与字节大小的发展历程
  • 2025年快递纸箱定做厂家权威推荐榜单:五层纸箱/重型纸箱/单层纸板箱源头厂家精选
  • 2025年镀锌角码实力厂家权威推荐榜单:万能立柱角码/角码连接件/钢结构预埋件源头厂家精选
  • Nmap 命令详细使用指南(官方参数全覆盖版) - 实践
  • selenium: 安装selenium
  • 基于单片机的故障检测自动保护智能防夹自动门设计及LCD状态显示架构
  • gpt安装 linux
  • GRANT语句在MySQL中的权限继承策略
  • 轨道平面系与轨道姿态系 - 实践
  • 51单片机(markdown格式阅读) - 实践
  • 【日记】博客爆炸了(1009 字)
  • 解决:部署mabayolo模型cd selective_scan pip install . cd ..报错 以及 torch.cuda.is_available()结果False
  • CMake构建学习笔记30-Ceres Solver库的构建
  • curl 命令使用笔记
  • 2025年口碑好的电厂清淤机器人厂家最新用户好评榜
  • 2025年靠谱的极低压抗污染反渗透膜厂家最新TOP排行榜
  • 2025年11月留学生回国求职机构推荐榜单:权威机构列表与选择指南
  • 2025年11月留学生回国求职机构推荐:五大权威机构榜单与选择指南
  • 2025年评价高的快速离心浓缩干燥器TOP品牌厂家排行榜
  • 2025年比较好的组合式恒温 振荡培养箱最新TOP品牌厂家排行
  • 2025年专业的输送机高评价厂家推荐榜
  • 2025年诚信的镁制硅晶防火风管厂家推荐及选购指南
  • 2025年靠谱的试验机行业内知名厂家排行榜
  • 2025年11月留学生找实习机构推荐:权威市场报告解读与口碑机构列表
  • 2025年质量好的自动叠皮机厂家推荐及采购指南
  • 2025年耐用的钢板预处理线TOP品牌厂家排行榜
  • 2025年口碑好的F40喷涂四氟厂家最新TOP排行榜