Codeforces 1077 F2 - Pictures with Kittens (hard version)
F2 - Pictures with Kittens (hard version)
思路:
单调队列优化dp
代码:
#pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/stdc++.h> using namespace std; #define fi first #define se second #define pi acos(-1.0) #define LL long long //#define mp make_pair #define pb push_back #define ls rt<<1, l, m #define rs rt<<1|1, m+1, r #define ULL unsigned LL #define pll pair<LL, LL> #define pli pair<LL, int> #define pii pair<int, int> #define piii pair<pii, int> #define mem(a, b) memset(a, b, sizeof(a)) #define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout); //head const int N = 5e3 + 10; const LL INF = 0x3f3f3f3f3f3f3f3f; int a[N]; deque<pair<int, LL>> dp[N]; LL tmp[N]; int main() { int n, k, x; scanf("%d %d %d", &n, &k, &x); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); dp[0].pb({0, 0}); for (int i = 1; i <= n; i++) { // i-k for (int l = 1; l <= x; l++) { while(!dp[l-1].empty() && dp[l-1].front().fi < i-k) dp[l-1].pop_front(); if(!dp[l-1].empty()) tmp[l] = dp[l-1].front().se + a[i]; } for (int l = 1; l <= x; l++) { while(!dp[l].empty() && dp[l].back().se <= tmp[l]) dp[l].pop_back(); if(tmp[l])dp[l].push_back({i, tmp[l]}); tmp[l] = 0; } } LL ans = -1; while(!dp[x].empty() && dp[x].front().fi <= n-k) dp[x].pop_front(); if(!dp[x].empty()) ans = dp[x].front().se; printf("%lld\n", ans); return 0; }