STL蒟蒻被hack了求助

P2627 [USACO11OPEN] Mowing the Lawn G

LoveYuigahamaYui @ 2024-07-06 11:23:42

#include <iostream>
#include <deque>
using namespace std;

typedef long long ll;

const int N = 1e5 + 10;
ll n, k, s[N], dp[N];

deque <int> q;

int main()
{
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) 
    {
        cin >> s[i];
        s[i] += s[i-1];
    }
    for (int i = 0; i <= n; ++i)
    {
        if (q.size() && i - q.front() > k) q.pop_front();
        if (i <= k) dp[i] = s[i];
        else if (q.size()) dp[i] = s[i] + dp[q.front() - 1] - s[q.front()];
        while (q.size() && dp[q.back() - 1] - s[q.back()] <= dp[i - 1] - s[i]) q.pop_back();
        q.push_back(i);
    }
    cout << dp[n];
}

by _Liyx_ @ 2024-07-06 17:34:30

@LoveYuigahamaYui 在第26行后加一个dp[i]=max(dp[i-1],dp[i])


by _Liyx_ @ 2024-07-06 17:34:54

这是经验


by LoveYuigahamaYui @ 2024-07-07 15:53:53

@Liyx 多谢多谢


|