Rise in Price(杭电多校DP)

题目:https://acm.hdu.edu.cn/showproblem.php?pid=6981

题意:给a,b俩个nn的数组,意味着每个i,j位置都会有两个数,从(1,1)开始走,只能往右或者往下走,每走一个格子就能将该位置的a数组的数和b数组的数分别叠加起来,num1代表求从(1,1)走到(n,n),a数组叠起来的值,num2代表b数组叠加起来的值,求一条路径,使得从(1,1)走到(n,n)num1num2最大值是多少。

题解:dp,参考官方题解:
在这里插入图片描述

代码:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
//#include <unordered_map>
//#include <unordered_set>
//#include <bits/stdc++.h>
//#define int long long
#define pb push_back
#define pii pair<int, int>
#define mpr make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define x first
#define y second
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int N = 100 + 9;
using namespace std;
inline int read() {
    char ch = getchar();
    int s = 0, w = 1;
    while (ch < '0' || ch > '9') {
        if (ch == '-') w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        s = s * 10 + ch - '0', ch = getchar();
    }
    return s * w;
}
int m, a[N][N], b[N][N], n;
vector<pii> f[N][N];
pii pool[N * 10000];

inline void check(pii t) {
    //如果第二维比t小,并且因为是小的先进,那么这个m位置的
    //第一维*第二维一定小于t的第一维乘以第二维,所以m这个数就没意义了,剔除
    while (m && pool[m].y <= t.y) m--;
    pool[++m] = t;  //加入新数
}
//这个操作就是将无论是第一维还是第二维都比某个数的第一维第二维小的数删掉
//因为这个数往后走一定没有那个数高
inline void judge(vector<pii> &A, vector<pii> &B, vector<pii> &C) {
    m = 0;
    int lenA = A.size(), lenB = B.size(), i = 0, j = 0;
    //第一维小的先进,这样就造成了pool第一维是递增的,再配合check里面的那个while循环
    //将会造成pool里的第二维是递减的,而c都是用pool来的,这就造成,我构造出来的c
    //第一维是递减的,第二维是递增的,然后我这个c被其它位置当做A或者B用的时候
    //我的第一维就是递增的
    while (i < lenA && j < lenB) check(A[i].x < B[j].x ? A[i++] : B[j++]);
    while (i < lenA) check(A[i++]);
    while (j < lenB) check(B[j++]);
    C.clear();//将C情况,重新添加,因为最外层有个T,所以要清空
    //将这m个数加入
    for (int i = 0; i < m; i++) C.push_back(pool[i + 1]);
}
signed main() {
    int T;
    T = read();
    while (T--) {
        n = read();
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++) a[i][j] = read();
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++) b[i][j] = read();
        f[1][1].clear();
        f[1][1].push_back(pii(a[1][1], b[1][1]));
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (i == 1 && i == j) continue;
                if (i == 1)
                    f[i][j] = f[i][j - 1];
                else if (j == 1)
                    f[i][j] = f[i - 1][j];
                else {
                    judge(f[i - 1][j], f[i][j - 1], f[i][j]);
                }
                int len = f[i][j].size();
                for (int k = 0; k < len; k++)
                    f[i][j][k].x += a[i][j], f[i][j][k].y += b[i][j];
            }
        }
        ll maxx = 0;
        for (int i = 0; i < f[n][n].size(); i++) {
            maxx = max(maxx, (ll)f[n][n][i].x * f[n][n][i].y);
        }
        printf("%lld\n", maxx);
    }
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值