小白进阶之路-滑雪-暑假训练

搜索依然爆炸的烂,继续加油吧

题解:按照 h 排序的大根堆,每次寻找四周 h 小的接上dp串。

#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

/*
 * time: 2020.7.23
 * thinking: 从 h 大的向 h 小的寻找,接上上一个的最长串
 * fighting and no pains no gains
 */
int a[200][200], dp[200][200];
int n, m;

struct node {
    int x, y, h;

    bool operator<(const node &cmp) const { // 自定义排序函数,按 h 划分的大根堆
        return h < cmp.h;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    priority_queue<node> q;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
            dp[i][j] = 1; // 最小长度就是本身
            q.push({i, j, a[i][j]});
        }
    }
    int Max = -1;
    while (!q.empty()) {
        int xx = q.top().x, yy = q.top().y, hh = q.top().h;
        cout << hh << endl;
        q.pop();
        int x = 0, b = 0, c = 0, d = 0;
        if (a[xx - 1][yy] > hh) x = dp[xx - 1][yy];
        if (a[xx][yy - 1] > hh) b = dp[xx][yy - 1];
        if (a[xx + 1][yy] > hh) c = dp[xx + 1][yy];
        if (a[xx][yy + 1] > hh) d = dp[xx][yy + 1];
        dp[xx][yy] = max(x, max(b, max(c, d))) + 1;
        Max = max(dp[xx][yy], Max);
    }
    cout << Max << endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Wise-XiaoWei4/p/13368583.html