问题求解

P1434 [SHOI2002] 滑雪

Sq6666 @ 2024-01-30 00:51:56

#include <iostream>
using namespace std;
#include <queue>  //将点按照值的升序进行排列[考虑dp的无后效性]
typedef pair<int, int> PII;
typedef pair<int, PII> PIPII;
priority_queue<PIPII,vector<PIPII>,greater<PIPII>> q;  // greater表示按pair排序方式,从小到大
const int N = 110;
int g[N][N];
int f[N][N];
int r, c;
int main()
{
    scanf("%d%d", &r, &c);

    for (int i = 1; i <= r; ++i)
    {
        for (int j = 1; j <= c; ++j)
        {
            f[i][j] = 1;
            scanf("%d", &g[i][j]);
            q.push({ g[i][j], { i,j } });
        }
    }

    //从小到大遍历mp,更新长度

    int dx[] = { -1,1,0,0 }, dy[] = { 0,0,-1,1 };
    while(!q.empty())
    {
        //出队
        auto t = q.top();
        q.pop();
        //取值
        int v = t.first;
        //坐标
        int x = t.second.first, y = t.second.second;
        //更新f[x][y]
        for (int i = 0; i < 4; ++i)
        {
            int a = x + dx[i], b = y + dy[i];
            if (v > g[a][b])
                f[x][y] = max(f[x][y], f[a][b] + 1);
        }
    }
    int res = 0;
    for (int i = 1; i <= r; ++i)
    {
        for (int j = 1; j <= c; ++j)
        {
            res = max(res, f[i][j]);
        }
    }
    printf("%d", res);
    return 0;
}

if (v > g[a][b]) f[x][y] = max(f[x][y], f[a][b] + 1);这里不已经保证是从小状态更新到大状态吗为什么还需小根堆


|