本题还有一个低配版:1049. 大盗阿福
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int n, k;
int m[N], p[N], f[N]; //f[i]表示在第i个点开餐馆的最大利润
int main()
{
int T;
cin >> T;
while (T -- )
{
cin >> n >> k;
for (int i = 1; i <= n; i ++ ) cin >> m[i];
for (int i = 1; i <= n; i ++ ) cin >> p[i];
for (int i = 1, j = 1, maxf = 0; i <= n; i ++ )
{
while (m[i] - m[j] > k) //判断两家餐馆距离是否大于k
{
maxf = max(maxf, f[j]); //maxf储存i的上上个点,即(i - 2)点的最大值
j ++ ;
}
f[i] = maxf + p[i]; //选了i点,(i - 1)点就不能选
}
int res = 0; //遍历所有状态找最大值
for (int i = 1; i <= n; i ++ ) res = max(res, f[i]);
cout << res << endl;
}
return 0;
}