跳到正文

题解

题解:P15967 采矿文明

Na1L0n9约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年3月29日
收录
2026年6月21日
更新
2026年11月13日
篇幅
约 2 分钟
题目
P15967

版本

  1. v1当前

Solution

闲话

自从 CSP2025 过后再也没有写过题解,今天心血来潮写一篇。

思路

注意到采矿的花费等于采矿的收益,且我们只关心收益总和,所以我们一定优先选择 aia_i 最大的矿井,直到无法满足题目的约束。

我们维护一个小根堆,里面存放当前被选中的 aia_i,且满足这个堆的元素个数不超过 kk,元素之和不超过当前剩余的金钱,如果元素个数超过了 kk,那就把贡献最小的弹出,如果钱不够了就直接结束,最后采矿量就是选中矿井的矿石数量与剩余金钱的最小值。

Code

C++
#include <bits/stdc++.h>
#define Write ios::sync_with_stdio(0);
#define by cin.tie(0);
#define Na1L0n9 cout.tie(0);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e5 + 10;
const int MOD = 998244353;
ll n, m, k, c[N], a[N];
priority_queue <ll, vector<ll>, greater<ll> > q;
int main() {
	Write by Na1L0n9
	cin >> n >> m >> k;
	for (int i = 1; i < n; i++) {
		cin >> c[i];
		c[i] += c[i - 1]; // 前缀和方便后面快速计算剩余的金钱
	}
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	ll ans = 0, tot = 0;
    for (int i = 1; i <= n; i++) {
        ll d = m - c[i - 1];
        if (d < 0) { // 钱不够了 
        	break;
		} 
        q.push(a[i]);
        tot += a[i];
        if (q.size() > k) { // 开采矿井数超过k 
            tot -= q.top();
            q.pop();
        }
        ans = max(ans, min(tot, d)); // 更新答案
    }
    cout << ans;
	return 0;
} // 喵!喵!
原文