80pts,求助,感谢

P1434 [SHOI2002] 滑雪

Habseligkeit @ 2023-02-24 19:44:07

具体思路就是将高度从大到小排序,然后从高处开始dp,这样可以保证无后效性


#include<bits/stdc++.h>
using namespace std;

struct node {
    int x;
    int y;
    int num;
}a[110];
int f[110][110] , g[110][110] , n , m;

bool cmp(node a1 , node a2) {
    if(a1.num == a2.num) return a1.x < a2.x;
    return a1.num > a2.num;
}

void init() {
    cin >> n >> m;
    for(int i = 1; i <= n; i ++)
    for(int j = 1; j <= m; j ++) {
        cin >> g[i][j];
        f[i][j] = 1;
        int k = (i - 1) * m + j; 
        a[k].x = i;
        a[k].y = j;
        a[k].num = g[i][j];
    }
    sort(a + 1 , a + n * m + 1 , cmp);
}

void dp() {
    for(int k = 1; k <= n * m; k ++) {
        int i = a[k].x , j = a[k].y;
        if(g[i + 1][j] > g[i][j]) f[i][j] = max(f[i + 1][j] + 1 , f[i][j]);
        if(g[i - 1][j] > g[i][j]) f[i][j] = max(f[i - 1][j] + 1 , f[i][j]);
        if(g[i][j + 1] > g[i][j]) f[i][j] = max(f[i][j + 1] + 1 , f[i][j]);
        if(g[i][j - 1] > g[i][j]) f[i][j] = max(f[i][j - 1] + 1 , f[i][j]);
    }
    int ans = -1;
    for(int i = 1; i <= n; i ++)
    for(int j = 1; j <= m; j ++)
    ans = max(ans , f[i][j]);
    cout << ans << endl;
}

int main() {
    init();
    dp();
    return 0;
}

by undifined @ 2023-02-25 23:37:25

数组a开小了


by Inferior_dust @ 2023-07-24 20:21:01

@undifined WOW草,真是一语惊醒梦中人!


|