# 「luogu3974」[TJOI2015] 组合数问题

## $Solution$：

$f_{i, j}$ 表示以 $(i, j)$ 为左下角的矩形里的最长反链：

$f_{i,j} = \max \big\{ f_{i - 1, j + 1} + a_{i,j}, \ \max \{ { f_{i - 1, j}, f_{i, j + 1} } \} \big\}$

## $Source$：

#include <cstdio>
#include <cstring>
#include <algorithm>
int in() {
int x = 0; char c = getchar(); bool f = 0;
while (c < '0' || c > '9')
f |= c == '-', c = getchar();
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return f ? -x : x;
}
template<typename T>inline void chk_min(T &_, T __) { _ = _ < __ ? _ : __; }
template<typename T>inline void chk_max(T &_, T __) { _ = _ > __ ? _ : __; }

const int N = 1e3 + 5;

int n, m, mp[N][N];
long long f[N][N];

int main() {
//freopen("in", "r", stdin);
int tim = in();
while (tim--) {
n = in(), m = in();
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
mp[i][j] = in();
for (int i = 1; i <= n; ++i)
for (int j = m; j; --j) {
f[i][j] = f[i - 1][j + 1] + mp[i][j];
chk_max(f[i][j], f[i][j + 1]);
chk_max(f[i][j], f[i - 1][j]);
}
printf("%lld\n", f[n][1]);
}
return 0;
}

posted @ 2019-10-23 10:14  15owzLy1  阅读(...)  评论(...编辑  收藏