You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 200010;
int f[N];
int w[N], n, m;
// 通过了 11/12个数据
int main() {
/**
普通DP大法
状态表示:
①集合:f[i]表示前i个灯塔满足条件并且i点亮。
②属性:最小代价
状态计算f[i]=min(f[j]+w[i]) (im≤j≤i1)
答案:(nm+1)~n必须有灯塔亮所以枚举一下求出最小值即可
*/
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> w[i];
//初始化
memset(f, 0x3f, sizeof f); //预求最小,先设最大
f[0] = 0; //需要手动初始化一下递推的base case
//依题意f[1]代表第1个灯塔点亮需要付出的代价是w[1],也就是f[0]=0,想想也正常虚拟第0个点亮成本为0~
for (int i = 1; i <= n; i++)
for (int j = max(0, i - m); j <= i - 1; j++) //向前查看它之前的m个
f[i] = min(f[i], f[j] + w[i]);
int res = INF;
for (int i = n + 1 - m; i <= n; i++) res = min(res, f[i]);
printf("%d\n", res);
return 0;
}