CF1098D Eels

题意

\(q\) 次询问,每次向集合 \(S\) 内添加一个元素或删除一个元素。

每次操作选择 \(x, y \in S, x \le y\),若 \(y \le 2 \times x\) 则称该操作对答案有 \(1\) 贡献。

求使得贡献最大的方案。

Sol

集中注意力。

每次合并最小的两个元素,答案一定不劣。

证明:考虑现在有最小的两个元素 \(a, b\),若 \(b\)\(x, y\) 合并而来,且 \(a, b\) 合并无贡献,可以发现需要满足 \(y \le a\),但是显然最小的 \(y = \frac{b}{2}\),而 \(a < \frac{b}{2}\),矛盾,因此得证。

考虑对于每个 \(2 ^ i\) 分段。

注意到对于每段只有第一个元素有贡献,且有贡献当且仅当前面所有数之和乘二依然小于当前的元素。

随便拿 \(30\)\(\text{multiset}\) 维护,做完了。

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <cmath>
#include <set>
#define int long long
#define pii pair <int, int>
using namespace std;
#ifdef ONLINE_JUDGE

/* #define getchar() (p1 == p2 && (p2 = (p1 = buf) = fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++) */
/* char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf; */

#endif
int read() {
	int p = 0, flg = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
		if (c == '-') flg = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
		p = p * 10 + c - '0';
		c = getchar();
	}
	return p * flg;
}
void write(int x) {
	if (x < 0) {
		x = -x;
		putchar('-');
	}
	if (x > 9) {
		write(x / 10);
	}
	putchar(x % 10 + '0');
}
bool _stmer;

#define fi first
#define se second

array <multiset <int>, 31> isl;
array <int, 31> psl;

bool _edmer;
signed main() {
	cerr << (&_stmer - &_edmer) / 1024.0 / 1024.0 << "MB\n";
	int q = read(), ans = 0;
	while (q--) {
		char strbuf[2];
		scanf("%s", strbuf);
		int x = read(), pos = log2(x);
		if (strbuf[0] == '+') psl[pos] += x, isl[pos].insert(x), ans++;
		else psl[pos] -= x, isl[pos].erase(isl[pos].lower_bound(x)), ans--;
		int _ans = 0, tp = 0;
		for (int i = 0; i <= 30; i++) {
			if (isl[i].empty()) continue;
			if (2ll * tp < *isl[i].begin())
				_ans++;
			tp += psl[i];
		}
		write(ans - _ans), puts("");
	}
	return 0;
}
posted @ 2024-04-26 23:42  cxqghzj  阅读(2)  评论(0编辑  收藏  举报