1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include <iostream> #include <algorithm> #include <cstring> #include <cstdio>
using namespace std;
const int maxn = 55; const int maxm = 4005;
int n, m, val[maxm], cnt[maxn][maxm]; int f[maxn][maxn][maxm], pos[maxn][maxn][maxm], mp[maxn][maxn][maxm]; int a[maxm], b[maxm], c[maxm], cpy[maxm], tot; int ans[maxn];
void calc(int l, int r, int v) { if (l > r) return; int p = pos[l][r][v], rk = mp[l][r][v]; ans[p] = cpy[rk]; calc(l, p - 1, rk); calc(p + 1, r, rk); }
int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; ++i) scanf("%d%d%d", &a[i], &b[i], &c[i]), cpy[i] = c[i]; sort(cpy + 1, cpy + 1 + m); tot = unique(cpy + 1, cpy + 1 + m) - (cpy + 1); for (int i = 1; i <= m; ++i) c[i] = lower_bound(cpy + 1, cpy + 1 + tot, c[i]) - cpy; for (int len = 1; len <= n; ++len) for (int l = 1; l <= n - len + 1; ++l) { int r = l + len - 1; for (int i = l; i <= r; ++i) for (int j = 1; j <= tot; ++j) cnt[i][j] = 0; for (int i = 1; i <= m; ++i) if (a[i] >= l && b[i] <= r) for (int j = a[i]; j <= b[i]; ++j) cnt[j][c[i]]++; for (int i = l; i <= r; ++i) for (int j = tot; j; --j) cnt[i][j] += cnt[i][j + 1]; for (int j = tot; j; --j) { pos[l][r][j] = l, mp[l][r][j] = j; for (int i = l; i <= r; ++i) { int tmp = f[l][i - 1][j] + f[i + 1][r][j] + cnt[i][j] * cpy[j]; if (tmp > f[l][r][j]) f[l][r][j] = tmp, pos[l][r][j] = i; } if (f[l][r][j + 1] > f[l][r][j]) f[l][r][j] = f[l][r][j + 1], pos[l][r][j] = pos[l][r][j + 1], mp[l][r][j] = mp[l][r][j + 1]; } } printf("%d\n", f[1][n][1]); calc(1, n, 1); for (int i = 1; i <= n; ++i) printf("%d ", ans[i]); return 0; }
|