P2627 [USACO11OPEN] Mowing the Lawn G
设
然后化简一下
对于
//#define FILE_INPUT
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define int long long
using namespace std;
bool Mbe;
#define rep(i, a, b) for (int i = a, END##i = b; i <= END##i; i++)
#define per(i, a, b) for (int i = a, END##i = b; i >= END##i; i--)
#define DEBUG(x) cerr << #x << " = " << x << '\n'
using LL = long long;
using ULL = unsigned long long;
inline LL read() {
LL s = 0, fu = 1; char ch = getchar();
while (ch < '0' || ch > '9') ch == '-' ? fu = -1 : 0, ch = getchar();
while (ch >= '0' && ch <= '9') s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
return s * fu;
}
const int Mod = 1e9 + 7;
const int Inf = 0x3f3f3f3f;
const LL InfLL = 0x3f3f3f3f3f3f3f3f;
const int N = 1e5 + 10;
int n, k, a[N], f[N], g[N], sum[N], q[N], hh, tt; // 留个下界 0
void Init() {
}
void Solve() {
n = read(), k = read();
rep(i, 1, n) a[i] = read(), sum[i] = a[i] + sum[i - 1];
rep(i, 1, n) {
g[i] = max(g[i - 1], f[i - 1]);
while (hh <= tt && i - k > q[hh]) hh++;
f[i] = g[q[hh]] - sum[q[hh]] + sum[i];
while (hh <= tt && g[q[tt]] - sum[q[tt]] < g[i] - sum[i]) tt--;
q[++tt] = i;
}
rep(i, 1, n) printf("%lld %lld\n", f[i], g[i]);
printf("%lld\n", max(f[n], g[n]));
}
bool Med;
signed main() {
#ifdef FILE_INPUT
freopen("input.in", "r", stdin);
#endif
int T = 1;
// T = read();
while (T--) {
Init();
Solve();
}
cerr << "Memory: " << fixed << setprecision(3) << (&Med - &Mbe) / 1048576.0 << " MB\n";
cerr << "Time: " << fixed << setprecision(3) << 1e3 * clock() / CLOCKS_PER_SEC << " ms\n";
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!