其他分享
首页 > 其他分享> > 烽火传递

烽火传递

作者:互联网

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

状态表示
状态转移
#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;
}

标签:int,tt,cin,long,传递,inf,烽火,define
来源: https://www.cnblogs.com/phr2000/p/14370572.html