30分玄学单队优化求助

P2627 [USACO11OPEN] Mowing the Lawn G

ZinfI_Sh @ 2024-12-28 15:01:38

难评,小数据一点一点调从10->20->30,大数据有过不了,单队写的很玄学

#include <bits/stdc++.h>
#define int long long
#define min(x, y) (x < y ? x : y)
#define max(x, y) (x > y ? x : y)
#define lowbit(x) (x & -x)
using namespace std;
const int DM[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};
const int HASHMOD = 9223372036854775783;
const int HASHBASE = 131;
const int HASHITEM = 999997;
int dp[100001][2], a[100001], sum[100001];
deque<int> q;
signed main()
{
    // freopen("edit.in", "r", stdin);
    // freopen("edit.out", "w", stdout);
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        sum[i] = sum[i - 1] + a[i];
        dp[i][1] = a[i];
    }
    int q0m = 0;
    for (int i = 1; i <= n; i++)
    {
        // cout << "origin front:" << q.front() << '\n';
        while (q.size() && q.front() < i - k + 1)
        {
            q.pop_front();
        }
        dp[i][0]=q0m;
        if (q.size())
        {
            int qf1 = q.front();
            // cout << i << " front " << qf1 << '\n';
            dp[i][1] = dp[qf1 - 1][0] + sum[i] - sum[qf1 - 1];
        }
        dp[i][1] = max(dp[i][1], dp[i - 1][0] + sum[i] - sum[i - 1]);
        while (q.size() && dp[q.back()][1] + sum[i] - sum[q.back()] < dp[i][1])
        {
            q.pop_back();
        }
        q0m = max(q0m, dp[i][1]);
        q.push_back(i);
    }
    // for (int i = 1; i <= n; i++)
    // {
    //     cout << dp[i][1] << ' ';
    // }
    // cout << '\n';
    // for (int i = 1; i <= n; i++)
    // {
    //     cout << dp[i][0] << ' ';
    // }
    // cout << '\n';
    cout << max(dp[n][0], dp[n][1]);
}

|