P2627 [USACO11OPEN] Mowing the Lawn G

fi 表示对于前 i 个数,i 选了, gi 表示对于前 i 个数,i 没选,sumi=j=1iej 我们可以列出状态转移方程。

gi=max{gi1,fi1}fi=maxikj<i{gj+(sumisumj)}

然后化简一下

gi=max{gi1,fi1}fi=maxikj<i{gjsumj}+sumi

对于 g 直接做,对于 f 可以用滑动窗口,单调队列来做。

//#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;
}
posted @   wh2011  阅读(1)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示