跳到正文

题解

题解:P10093 [ROIR 2022 Day 2] 礼物

Xy_top约 7 分钟
原文
文章信息与版本

文章信息

发布
2024年7月23日
收录
2026年3月21日
更新
2026年11月14日
篇幅
约 7 分钟
题目
P10093

版本

  1. v1当前

这个题是恶评的吧,120120 行代码的蓝题?细节还这么多。

先考虑第 kk 大的套路,在此题中,观察到 kk 很小,容易想到枚举第 kk 大。

将所有数字从大到小排序后依次插入,当插入的数字数量 ≥k+1\geq k+1 时,可以钦定当前插入的数字 xx 为第 k+1k+1 大。在这个数字的左侧选择 LL 个连续且比 xx 大的数 p1...pLp_1...p_L,右侧同理选择 RR 个,为 q1...qRq_1...q_R,使得 L+R=kL+R=k ,这样当前数字就是第 k+1k+1 大。然后从 p1p_1 左侧选取一个连续段,但不要包含大于 xx 的,右侧同理,该过程可以通过 前缀和 以及 ST 表 实现。

为了方便找到左右侧连续比 xx 大的数,可以采用链表。维护链表可以采用 set。

卡常建议:ST 表 22 的 nn 次方那一项一定要放在前面,不要用 cin cout,再开 O2。

talk is cheap, show me the code:

C++
#include <bits/stdc++.h>
#define int long long
#define For(i, a, b) for (int i = (a); i <= (b); i ++)
#define foR(i, a, b) for (int i = (a); i >= (b); i --)
using namespace std;
struct Node {int val, loc;}a[200005];
bool cmp (Node n1, Node n2) {return n1.val > n2.val;}
set <pair <int, int> > s;
int n, k, cnt = 1;
int lst[200005], nxt[200005], lis[200005][2];
int lg[200005], pre[18], mi[18][200005], mx[18][200005];
void initst () {
	For (j, 1, 17) {
		for (int i = 0; i + pre[j] - 1 <= n + 1; i ++) {
			mi[j][i] = min (mi[j - 1][i], mi[j - 1][i + pre[j - 1] ]);
			mx[j][i] = max (mx[j - 1][i], mx[j - 1][i + pre[j - 1] ]);
		}
	}
}
int qmin (int x, int y) {
	if (x > y) return 0;
	int l = lg[y - x + 1];
	return min (mi[l][x], mi[l][y - pre[l] + 1]);
}
int qmax (int x, int y) {
	if (x > y) return 0;
	int l = lg[y - x + 1];
	return max (mx[l][x], mx[l][y - pre[l] + 1]);
}
int qsum (int x, int y) {
	return mx[0][y] - mi[0][x - 1];
}
void solve () {
	lg[1] = 0;
	For (i, 0, 17) pre[i] = 1 << i;
	For (i, 2, 200000) lg[i] = lg[i / 2] + 1;
	scanf ("%lld%lld", &n, &k);
	For (i, 1, n) {
		scanf ("%lld", &a[i].val);
		mi[0][i] = mi[0][i - 1] + a[i].val;
		mx[0][i] = mx[0][i - 1] + a[i].val;
		a[i].loc = i;
	}
	mi[0][n + 1] = mi[0][n];
	mx[0][n + 1] = mx[0][n];
	if (k == 0) {
		int mx = -1000000000, ans = 0, sum = 0;
		For (i, 1, n) {
			sum += a[i].val;
			mx = max (mx, a[i].val);
			sum = max (sum, 0LL);
			ans = max (ans, sum);
		}
		if (mx < 0) printf ("%lld", mx);
		else printf ("%lld", ans);
		return;
	}
	initst ();
	sort (a + 1, a + n + 1, cmp);
	For (i, 1, n) {
		lis[i][0] = a[i].val;
		lis[i][1] = a[i].loc;
	}
	s.insert (make_pair (a[1].loc, 1LL) );
	int ans = 0;
	For (i, 2, n) {
		++ cnt;
		s.insert (make_pair (a[i].loc, i) );
		auto it = s.find (make_pair (a[i].loc, i) );
		if (it != s.begin () ) {
			-- it;
			int x = (it -> second);
			lst[i] = x;
			nxt[x] = i;
			++ it;
		}
		++ it;
		if (it != s.end () ) {
			int x = (it -> second);
			nxt[i] = x;
			lst[x] = i;
		}
 		if (i >= k + 1) {
			int l = i, lcnt = 0, r = i, rcnt = 0, s = 0;
			while (lst[l] && lcnt < k) {
				l = lst[l];
				s += lis[l][0];
				++ lcnt;
			}
			while (nxt[r] && lcnt + rcnt < k) {
				r = nxt[r];
				s += lis[r][0];
				++ rcnt;
			}
			int x = lis[l][1], y = lis[r][1];
			ans = max (ans, qsum (x, y) + mi[0][x - 1] - qmin ( (!lst[l] ? 0LL : lis[lst[l] ][1]), x - 1) + qmax (y, (!nxt[r] ? n + 1 : lis[nxt[r] ][1] - 1) ) - mx[0][y] - s);
			while (nxt[r]) {
				s -= lis[l][0];
				l = nxt[l];
				r = nxt[r];
				s += lis[r][0];
				int x = lis[l][1], y = lis[r][1];
				ans = max (ans, qsum (x, y) + mi[0][x - 1] - qmin (lis[lst[l] ][1], x - 1) + qmax (y, (!nxt[r] ? n : lis[nxt[r] ][1] - 1) ) - mx[0][y] - s);
				if (l == i) break;
			}
		}
	}
	printf ("%lld", ans);
}
signed main () {
	int _ = 1;
//	cin >> _;
	while (_ --) {
		solve ();
		cout << '\n';
	}
	return 0;
}
原文