跳到正文

题解

题解:CF2200F Mooclear Reactor 2

小屹0_0约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年3月23日
收录
2026年9月22日
更新
2026年11月16日
篇幅
约 10 分钟
获赞
5
题目
CF2200F

版本

  1. v1当前

前言

嘟嘟嘟的嘟嘟嘟题。

思路

这种题目的套路就是枚举最小的限制,对于这个题也就是枚举最小的 yy 所在的位置。

于是考虑排序,按照 yy 从大到小排序。

接下来就考虑如何维护一个前缀的答案(不考虑加入的商品)。

不难发现,如果 1∼i1 \sim i 的决策中丢弃了点 kk,那么由于 yi+1≤yiy_{i+1} \leq y_i,所以 1∼i+11 \sim i + 1 也不会选择 kk。

于是容易想到使用优先队列直接维护。

接下来考虑询问。

注意到如果直接处理前缀,还需要二分当前位置会在原序列中插入到哪里,剩下的东西还不好维护。

考虑对于每一个最小的 yy 维护答案。

维护的方式是类似的,不过需要多维护一些东西,以便于计算答案,接下来讲详细讲解。

不难发现,我们肯定会选择替换原序列最小值为 yy 对应的选择中的最小值。

设 f(y)f(y) 表示选了小于等于 y+1y + 1 个数最小值为 yy 的时候的最大方案。

设 g(y)g(y) 表示选了小于等于 yy 个数最小值为 yy 的时候的最大方案。

不难发现,ff 就是我们之前维护的东西,gg 也是好维护的。如果 f(y)f(y) 的最大值选了 y+1y+1 个数,那么 g(y)g(y) 就是 f(y)−mnf(y) - mn,其中 mnmn 是最小的选择的数,否则 g(y)=f(y)g(y) = f(y)。

于是对于一个询问 (x,y)(x,y),答案就是 max⁡{ans,g(y)+x}\max \{ ans, g(y) + x \},其中 ansans 就是不选任何其它数的答案。

代码

核心代码
C++
cin >> n >> m;
for (int i = 1; i <= n; i ++ ) cin >> a[i].fi >> a[i].se;
sort(a + 1, a + n +1, cmp);

priority_queue <int, vector <int>, greater <int> > q;
int j = 1, all = 0, now = 0, ans = 0;
for (int i = n; i >= 0; i -- ){
	while (j <= n && a[j].se >= i) now ++, all += a[j].fi, q.push(a[j].fi), j ++;
	while (q.size() > i + 1) now --, all -= q.top(), q.pop();
	
	if (now == i + 1) sum[i] = all - q.top();
	else sum[i] = all;
	
	ans = max(ans, all);
}

pre[0] = sum[0];
for (int i = 1; i <= n; i ++ ) pre[i] = max(pre[i - 1], sum[i]);

for (int i = 1; i <= m; i ++ ){
	int x, y; cin >> x >> y;
	cout << max(ans, pre[y] + x) << " ";
}

cout << "\n";
完整代码,仅供娱乐
C++
#include <bits/stdc++.h>
using namespace std;

#define cases int T; cin >> T; while (T -- )
#define int long long
#define ll long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define vi vector <int>
#define vl vector <ll>
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)

const int N = 1e6 + 5, mod = 1e9 + 7;

namespace Speed{
	inline ll read(){
		ll x = 0; char ch = getchar();
		while (ch < '0' || ch > '9') ch = getchar();
		while (ch >= '0' && ch <= '9') x = x * 10ll + ch - '0', ch = getchar();
		return x;
	}

	inline void write(ll x){
		if (x > 9) write(x / 10);
		putchar(x % 10 + '0');
	}
}
namespace Int{
	const int MAX = 1000000000;
	
	vi add(vi &a, vi &b){
		vi ans; int t = 0;
		for (int i = 0; i < a.size() || i < b.size() || t; i ++ ){
			if (i < a.size()) t += a[i];
			if (i < b.size()) t += b[i];
			ans.push_back(t % MAX), t /= MAX;
		}
		
		return ans;
	}
	vi sub(vi &a, vi &b){
		vi ans; int t = 0;
		for (int i = 0; i < a.size() || i < b.size() || t; i ++ ){
			if (i < a.size()) t += a[i];
			if (i < b.size()) t -= b[i];
			ans.push_back(t % MAX), t /= MAX;
		}
		
		return ans;
	}
	vi mul(vi &a, int b){
		vi ans; int t = 0;
		for (int i = 0; i < a.size() || t; i ++ ){
			if (i < a.size()) t += a[i] * b;
			ans.push_back(t % MAX), t /= MAX;
		}
		
		return ans;
	}
}
namespace Binary{
	int lowbit(int x){
		return x & (-x);
	}
	
	int popcount(int x){
		int k = 0;
		while (x) x -= lowbit(x), k ++;
		return k;
	}
};
namespace modulo{
	int inv[N], fac[N];
	
	int qpow(int a, int b){
		int res = 1;
		while (b){
			if (b & 1) res = 1ll * res * a % mod;
			a = 1ll * a *a % mod, b >>= 1;
		}
		return res;
	}
	
	void init_factors(int n){
		fac[0] = 1; for (int i = 1; i <= n; i ++ ) fac[i] = 1ll * fac[i - 1] * i % mod;
	}

	int C(int n, int m){
		if (n < m) return 0;
		return 1ll * fac[n] * qpow(fac[n - m], mod - 2) % mod * qpow(fac[m], mod - 2) % mod;
	}
	
	void add(int &a, int b){
		a += b;
		if (a >= mod) a -= mod;
		if (a < 0) a += mod;
	}
	
	void sub(int &a, int b){
		a -= b;
		if (a >= mod) a -= mod;
		if (a < 0) a += mod;
	}
	
	void mul(int &a, int b){
		a = 1ll * a * b % mod;
	}
	
	void bmul(int &a, int b){
		int t = a; a = 0;
		while (b){
			if (b & 1) add(a, t);
			add(t, t); b >>= 1;
		}
	}
}
namespace BIT{
	using namespace Binary;
	int maxn, tr[N];
	
	void modify(int pos, int x){
		while (pos <= maxn) tr[pos] += x, pos += lowbit(pos);
	}
	
	void modify_mod(int pos, int x){
		while (pos <= maxn) tr[pos] = (tr[pos] + x) % mod, pos += lowbit(pos);
	}
	
	int query(int pos, int x){
		int res = 0;
		while (pos) res += tr[pos], pos -= lowbit(pos);
		return res;
	}
	
	int query_mod(int pos, int x){
		int res = 0;
		while (pos) res = (res + tr[pos]) % mod, pos -= lowbit(pos);
		return res;
	}
}

int n, m;
pii a[N];

bool cmp(pii a, pii b){
	if (a.se == b.se) return a.fi > b.fi;
	return a.se > b.se;
}

int sum[N], pre[N];

signed main(){
	fast;
	
	cases{
		cin >> n >> m;
		for (int i = 1; i <= n; i ++ ) cin >> a[i].fi >> a[i].se;
		sort(a + 1, a + n +1, cmp);
		
		priority_queue <int, vector <int>, greater <int> > q;
		int j = 1, all = 0, now = 0, ans = 0;
		for (int i = n; i >= 0; i -- ){
			while (j <= n && a[j].se >= i) now ++, all += a[j].fi, q.push(a[j].fi), j ++;
			while (q.size() > i + 1) now --, all -= q.top(), q.pop();
			
			if (now == i + 1) sum[i] = all - q.top();
			else sum[i] = all;
			
			ans = max(ans, all);
		}
		
		pre[0] = sum[0];
		for (int i = 1; i <= n; i ++ ) pre[i] = max(pre[i - 1], sum[i]);
		
		for (int i = 1; i <= m; i ++ ){
			int x, y; cin >> x >> y;
			cout << max(ans, pre[y] + x) << " ";
		}
		
		cout << "\n";
	}
}

后记

嘟嘟嘟的嘟嘟嘟题。

原文