dp wa #2 & 8

P1434 [SHOI2002] 滑雪

long_int @ 2021-09-09 13:55:48


#include <stdio.h>
#include <algorithm>
#pragma warning (disable : 4996)
#define maxn 1005
#define sort std::sort
#define LL long long
struct coord { LL x, y, v; }a[maxn];
LL r, c;
int dx[] = { 1,0,-1,0 };
int dy[] = { 0,1,0,-1 };
LL m[maxn][maxn], dp[maxn][maxn];
bool cmp(coord a, coord b) {
    return a.v < b.v;
}
LL max(LL a, LL b) { return a > b ? a : b; }
int main() {
    int n = 0;
    scanf("%lld%lld", &c, &r);
    for (int i = 1; i <= r; i++)
        for (int j = 1; j <= c; j++) {
            scanf("%lld", &m[i][j]);
            a[++n].v = m[i][j];
            a[n].x = i, a[n].y = j;
        }
    sort(a + 1, a + 1 + n, cmp);
    for (int i = 1; i <= r; i++)
        for (int j = 1; j <= c; j++)
            dp[i][j] = 1;
    for (int i = n; i >= 1; i--) {
        for (int j = 0; j < 4; j++) {
            int x = a[i].x + dx[j], y = a[i].y + dy[j];
            if (x <= 0 || y <= 0 || x > r || y > c)
                continue;
            if (m[x][y] < a[i].v)
                dp[x][y] = max(dp[a[i].x][a[i].y] + 1, dp[x][y]);
        }
    }
    LL ans = 0;
    for (int i = 1; i <= r; i++)
        for (int j = 1; j <= c; j++)
            ans = max(ans, dp[i][j]);
    printf("%lld", ans);
    return 0;
}

by long_int @ 2021-09-09 13:58:19

第18行的r,c打反了,不过还是80


by long_int @ 2021-09-10 12:44:16

a数组开小了,已过


|