如果你 WA on #9

P2627 [USACO11OPEN] Mowing the Lawn G

uncesspath @ 2023-11-03 16:37:54

错误的代码:

int F(int i) { return i != 0 ? dp[i - 1] - e[i] : 0; }

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> e[i];
    for (int i = 1; i <= n; i++)
        e[i] += e[i - 1];
    q.push_back(0);
    for (int i = 1; i <= n; i++)
    {
        while (q.size() && i - q.front() > k)
            q.pop_front();
        while (q.size() && F(i) >= F(q.back()))
            q.pop_back();
        q.push_back(i);
        dp[i] = F(q.front()) + e[i];
    }
    cout << dp[n] << '\n';
    return 0;
}

这里 F(i) 要开 long long.


|