烽火传递

https://www.acwing.com/problem/content/1091/

状态表示
  • \(f[i]:点燃第i个且满足且每m个至少点燃一个的全体集合\)
  • \(属性:min\)
状态转移
  • \(f[i] = f[q[hh]]+f[i]\\\)
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define pb push_back
#define PII pair<int, int>
#define fi first
#define se second
#define inf 0x3f3f3f3f
const int N = 2e5 + 7;
int a[N], f[N];
int q[N];
           
int main() {
    IO;
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    int hh = 0, tt = 0;
    for (int i = 1; i <= n; ++i) {
        if (q[hh] < i - m) ++hh;
        f[i] = f[q[hh]] + a[i];
        while (hh <= tt && f[q[tt]] >= f[i]) --tt;
        q[++tt] = i;
    }
    int ans = inf;
    for (int i = n - m + 1; i <= n; ++i) ans = min(ans, f[i]);
    cout << ans << '\n';
    return 0;
}
posted @ 2021-02-04 01:56  phr2000  阅读(62)  评论(0编辑  收藏  举报