70分求助 思路:map排序,再dp

P1434 [SHOI2002] 滑雪

DarPluto_9 @ 2022-08-29 18:39:50

#include <iostream>
using namespace std;
#include <map>  //将点按照值的升序进行排列[考虑dp的无后效性]
typedef pair<int, int> PII;
map<int, PII> mp;  //第一个为该点的值,第二个为该点坐标
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]);
            mp.insert({ g[i][j], { i,j } });
        }
    }

    //从小到大遍历mp,更新长度
    int res = 0;
    int dx[] = { -1,1,0,0 }, dy[] = { 0,0,-1,1 };
    for (auto& t : mp)
    {
        //值
        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);
        }
        //更新res
        res = max(res, f[x][y]);
    }
    printf("%d", res);
    return 0;
}

by bamboo12345 @ 2022-08-29 19:14:03

@DarPluto_9 如果有重复的高度你想想会怎么样


by DarPluto_9 @ 2022-08-29 19:36:13

@bamboo123 思索良久,还是没想明白,还请大佬指点迷津,感谢感谢~


by bamboo12345 @ 2022-08-29 19:42:59

@DarPluto_9 你可以用map试一下,高度相同他只会保留最后一个你存的东西


by DarPluto_9 @ 2022-08-29 19:45:36

@bamboo123 好的,非常感谢


|