非常感谢各位的回答!
动态规划不来,只有用贪心了,
方法与7楼同。
动态规划不来,只有用贪心了,
方法与7楼同。
#include <stdio.h> #include <string.h> #define N 100 int d[N]; int dp[N][N] = {{0}}; int max(int l, int r) { int m = l++; for (; l <= r; l++) if (d[m] < d[l]) m = l; return d[m]; } int proc(int l, int r) { int i, m = 0, cur; if (l > r) return 0; if (dp[l][r]) return dp[l][r]; if (r - l < 3) return dp[l][r] = max(l , r); cur = 0x7FFFFFFF; for (i = l + 1; i <= r - 1; i++) { int ndp = proc(l, i - 2) + proc(i + 2, r); if (ndp < cur) cur = ndp, m = i; } return dp[l][r] = cur + max(m - 2, m + 2); } int main() { int r, c, n; while (scanf("%d %d %d", &r, &c, &n) == 3) { memset(d, 0, sizeof(d)); memset(dp, 0, sizeof(dp)); while (n--) { int x, y; scanf("%d %d", &y, &x); if (d[x-1] < y) d[x-1] = y; } printf("%d\n", proc(0, c - 1)); } return 0; }