1 条题解

  • 0
    @ 2026-11-2 13:31:55

    修剪草坪(Mowing the Lawn G) 题解

    思路分析

    按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。

    参考代码

    #include <iostream>
    #include <vector>
    #include <deque>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n, k;
        if (!(cin >> n >> k)) return 0;
        vector<long long> e(n + 2, 0), dp(n + 2, 0);
        long long tot = 0;
        for (int i = 1; i <= n; ++i) {
            cin >> e[i];
            tot += e[i];
        }
        deque<int> dq;
        dq.push_back(0);
        for (int i = 1; i <= n + 1; ++i) {
            while (!dq.empty() && dq.front() < i - k - 1) dq.pop_front();
            dp[i] = dp[dq.front()] + e[i];
            while (!dq.empty() && dp[dq.back()] >= dp[i]) dq.pop_back();
            dq.push_back(i);
        }
        cout << tot - dp[n + 1] << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    8711
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者