main.cc 922 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <algorithm>
  2. #include <cstdio>
  3. using namespace std;
  4. const int dx[4] = {0, 1, 0, -1};
  5. const int dy[4] = {1, 0, -1, 0};
  6. const int N = 100;
  7. int h[N][N], dp[N][N];
  8. struct Grid {
  9. int r;
  10. int c;
  11. int h;
  12. bool operator<(const Grid &that) const { return this->h < that.h; }
  13. };
  14. Grid area[N * N];
  15. int main() {
  16. int R, C;
  17. scanf("%d %d", &R, &C);
  18. for (int i = 0, k = 0; i < R; i++) {
  19. for (int j = 0; j < C; j++) {
  20. scanf("%d", &h[i][j]);
  21. dp[i][j] = 1;
  22. area[k++] = {i, j, h[i][j]};
  23. }
  24. }
  25. sort(area, area + R * C);
  26. int len = 1;
  27. for (int i = 0; i < R * C; i++) {
  28. Grid g = area[i];
  29. for (int j = 0; j < 4; j++) {
  30. int nx = g.r + dx[j], ny = g.c + dy[j];
  31. if (0 <= nx && nx < R && 0 <= ny && ny < C && g.h < h[nx][ny]) {
  32. dp[nx][ny] = max(dp[nx][ny], dp[g.r][g.c] + 1);
  33. len = max(len, dp[nx][ny]);
  34. }
  35. }
  36. }
  37. printf("%d\n", len);
  38. return 0;
  39. }