dp wa #2 & 8
查看原帖
dp wa #2 & 8
561708
long_int楼主2021/9/9 13:55
#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;
}
2021/9/9 13:55
加载中...