https://www.luogu.com.cn/problem/P3592
每个人都是选一段连续区间进行操作,可以考虑使用区间 DP。
首先可以对 进行离散化,因为只需要知道相对大小关系就能判断人是否能洗车。
每个人是否洗车,关心的是区间内最小值,于是记录 表示区间 内的洗车店最便宜的钱数为 元,人在这个区间内所花的最多钱数。注意这里人的洗车店区间是要严格被包含在区间 内的。
考虑如何转移,枚举区间内的最小值,以及最小值的位置就好。
其中,满足 ,, 表示满足洗车区间严格在 内同时包含点 ,且花的钱数大于等于 的人的个数。
暴力转移复杂度是 的,需要优化。
不难发现, 可以单独预处理,前半部分的 可以后缀和优化。
具体的,设 ,其中 。
原式变成:,复杂度降至 ,可以接受。
#include <bits/stdc++.h>
using namespace std;
const int N=55, M=4005;
struct node
{
int L, R, c;
}a[M];
int f[N][N][M], s[N][N][M];
int n, m, cnt[M], tot=0, res=0;
vector<int> vec;
void lsh()
{
for (int i=1; i<=m; i++) vec.push_back(a[i].c);
vec.push_back(-1);
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end()), tot=vec.size();
for (int i=1; i<=m; i++) a[i].c=lower_bound(vec.begin(), vec.end(), a[i].c)-vec.begin();
}
int main()
{
scanf("%d%d", &n, &m);
for (int i=1; i<=m; i++) scanf("%d%d%d", &a[i].L, &a[i].R, &a[i].c);
lsh();
for (int len=2; len<=n; len++)
for (int L=1, R=len; R<=n; L++, R++)
for (int k=L; k<=R; k++)
{
for (int c=1; c<=tot; c++) cnt[c]=0;
for (int i=1; i<=m; i++) if (a[i].L>=L && a[i].R<=R && a[i].L<=k && a[i].R>=k) cnt[a[i].c]++;
for (int c=tot; c>=1; c--) cnt[c]+=cnt[c+1];
for (int c=1; c<=tot; c++) f[L][R][c]=max(f[L][R][c], s[L][k-1][c]+s[k+1][R][c]+cnt[c]*vec[c]);
for (int c=tot; c>=1; c--) s[L][R][c]=max(s[L][R][c+1], f[L][R][c]);
}
for (int i=1; i<=tot; i++) if (f[1][n][i]>f[1][n][res]) res=i;
printf("%d\n", f[1][n][res]);
return 0;
}