跳到正文

个人记录

来追梦反悔贪心测试总结

_YaIkGl_约 21 分钟
原文
文章信息与版本

文章信息

收录
2026年7月16日
更新
2026年7月16日
篇幅
约 21 分钟

版本

  1. v1当前

D1

P16789 [蓝桥杯 2026 国 A] 核心任务调度

很明显的反悔贪心板子,需要注意一下排序顺序。用 pair 需谨慎/ll,下次应该仔细推推到底该怎么排序才行

赛时代码
C++
#include <bits/stdc++.h> 
#define int long long
using namespace std;


const int N = 2e5 + 10;
int n, ans1, ans2, cnt;
struct Node {
	int t, w, tp;
	bool operator < (const Node & x) const {
		if(t != x.t) {
			return t < x.t;
		}
		if(tp != x.tp) {
			return tp > x.tp;
		}
		return w > x.w;
	}
}a[N]; 
priority_queue<pair<int, int> > q;

signed main() {
	
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i].t >> a[i].w >> a[i].tp;
	}
	sort(a + 1, a + n + 1);
	for(int i = 1; i <= n; i++) {
		if(cnt < a[i].t) {
			cnt++;
			ans1 += a[i].w;
			ans2 += a[i].tp;
			q.push({a[i].tp, a[i].w});
		}
		else if(q.top().first < a[i].tp || q.top().first == a[i].tp && q.top().second < a[i].w) {
			ans1 += a[i].w - q.top().second;
			ans2 += a[i].tp - q.top().first;
			q.pop();
			q.push({a[i].tp, a[i].w});
		}
	}
	cout << ans2 << " " << ans1 << "\n";
	
}
正确代码
C++
#include <bits/stdc++.h> 
#define int long long
using namespace std;


const int N = 2e5 + 10;
int n, ans1, ans2, cnt;
struct Node1 {
	int t, w, tp;
	bool operator < (const Node1 & x) const {
		if(t != x.t) {
			return t < x.t;
		}
		if(tp != x.tp) {
			return tp > x.tp;
		}
		return w > x.w;
	}
}a[N]; 
struct Node2 {
	int first, second;
	bool operator < (const Node2 & x) const {
		if(first != x.first) {
			return first > x.first;
		}
		return second > x.second;
	}
}; 
priority_queue<Node2> q;

signed main() {
	
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i].t >> a[i].w >> a[i].tp;
	}
	sort(a + 1, a + n + 1);
	for(int i = 1; i <= n; i++) {
		if(cnt < a[i].t) {
			cnt++;
			ans1 += a[i].w;
			ans2 += a[i].tp;
			q.push({a[i].tp, a[i].w});
		}
		else if(q.top().first < a[i].tp || q.top().first == a[i].tp && q.top().second < a[i].w) {
			ans1 += a[i].w - q.top().second;
			ans2 += a[i].tp - q.top().first;
			q.pop();
			q.push({a[i].tp, a[i].w});
		}
	}
	cout << ans2 << " " << ans1 << "\n";
	
}

P3545 [POI 2012] HUR-Warehouse Store

这道题纯板子,就是需要用一个 set 维护一下答案。

我赛时忘记判断优先队列是否为空了导致 RE 了。。。

赛时代码
C++
#include <bits/stdc++.h> 
#define int long long
using namespace std;


const int N = 3e5 + 10;
int n, cnt, ans, a[N], b[N];
priority_queue<pair<int, int> > q;
set<int> s;

signed main() {
	
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	for(int i = 1; i <= n; i++) {
		cin >> b[i];
		cnt += a[i];
		if(cnt >= b[i]) {
			cnt -= b[i]; 
			ans++;
			q.push({b[i], i});
			s.insert(i);
		}
		else if(q.top().first > b[i] && cnt + q.top().first >= b[i]) {
			s.erase(q.top().second);
			s.insert(i);
			cnt -= b[i] - q.top().first;
			q.pop();
			q.push({b[i], i});
		}
	}
	cout << ans << "\n";
	while(s.size()) {
		cout << (*s.begin()) << " ";
		s.erase(s.begin());
	}
	cout << "\n";
	
}
正确代码
C++
#include <bits/stdc++.h> 
#define int long long
using namespace std;


const int N = 1e6 + 10;
int n, cnt, ans, a[N], b[N];
priority_queue<pair<int, int> > q;
set<int> s;

signed main() {
	
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	for(int i = 1; i <= n; i++) {
		cin >> b[i];
		cnt += a[i];
		if(cnt >= b[i]) {
			cnt -= b[i]; 
			ans++;
			q.push({b[i], i});
			s.insert(i);
		}
		else if(q.size() && q.top().first > b[i] && cnt + q.top().first >= b[i]) {
			s.erase(q.top().second);
			s.insert(i);
			cnt -= b[i] - q.top().first;
			q.pop();
			q.push({b[i], i});
		}
	}
	cout << ans << "\n";
	while(s.size()) {
		cout << (*s.begin()) << " ";
		s.erase(s.begin());
	}
	cout << "\n";
	
}

P3045 [USACO12FEB] Cow Coupons G

首先我们考虑优先选取折扣价低的牛,然后后续的牛有两种选择:

  1. 直接原价购买,此时明显优先选取剩余的牛中原价最小的购买。
  2. 把前面的一个已经用过优惠券的牛原价购买,然后把优惠券给这头牛使用。对于用过优惠券的牛,肯定要选择原价和折扣价差价最小的牛替换,并且新使用优惠券的牛也需要折扣价尽可能的低。

此时发现可以用三个优先队列完成此操作。

赛时代码
C++
#include <bits/stdc++.h> 
#define int long long
using namespace std;


const int N = 2e5 + 10;
int n, k, m, cnt, cost, ans;
pair<int, int> a[N];
priority_queue<pair<int, int> > q;

signed main() {
	
	cin >> n >> k >> m;
	for(int i = 1; i <= n; i++) {
		cin >> a[i].second >> a[i].first;
	}
	sort(a + 1, a + n + 1, [&](const pair<int, int> a, pair<int, int> b) {
		if(a.second - a.first != b.second - b.first) {
			return a.second - a.first > b.second - b.first;
		}
		return a < b;
	});
	for(int i = 1; i <= n; i++) {
		if(cnt < k && cost + a[i].first <= m) {
			ans++;
			cnt++;
			cost += a[i].first;
			q.push({a[i].first, 1});	
			continue;
		}
		else if(cost + a[i].second <= m) {
			ans++;
			cost += a[i].second;
			q.push({a[i].second, 0});
			continue;
		}
		pair<int, int> now = q.top();
		if(cnt - now.second < k && a[i].first < now.first) {
			q.pop();
			cnt += 1 - now.second;
			cost += a[i].first - now.first;
			q.push({a[i].first, 1});
		}
		else if(cnt - now.second >= k && a[i].second < now.first) {
			q.pop();
			cnt -= now.second;
			cost += a[i].second - now.first;
			q.push({a[i].second, 0});
		}
	}
	cout << ans << "\n";
	
}
正确代码
C++
#include <bits/stdc++.h> 
#define int long long
using namespace std;


const int N = 5e4 + 10;
int n, k, m, ans, cost, vis[N];
pair<int, int> a[N];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q1, q2, q3;

signed main() {
	
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
//	freopen("in.in", "r", stdin); 
	
	cin >> n >> k >> m;
	for(int i = 1; i <= n; i++) {
		cin >> a[i].first >> a[i].second;
		q1.push({a[i].second, i});
		q2.push({a[i].first, i});
	}
	for(int i = 1; i <= k; i++) {
		if(cost + q1.top().first > m) {
			break;
		}
		ans++;
		cost += q1.top().first;
		vis[q1.top().second] = 1;
		q3.push({a[q1.top().second].first - a[q1.top().second].second, q1.top().second});
		q1.pop();
	}
	if(ans < k) {
		cout << ans << "\n";
		return 0;
	}
	while(ans < n) {
		while(!q1.empty() && vis[q1.top().second]) {
			q1.pop();
		}
		while(!q2.empty() && vis[q2.top().second]) {
			q2.pop();
		}
		if(q2.empty()) {
			break;
		}
		int w1 = q1.top().first, w2 = q2.top().first, w3 = q3.top().first;
		int id1 = q1.top().second, id2 = q2.top().second, id3 = q3.top().second;
		if(min(w1 + w3, w2) + cost > m) {
			break;
		}
		if(w1 + w3 > w2) {
			ans++;
			vis[id2] = 1;
			cost += w2;
			q2.pop();
		}
		else {
			ans++;
			vis[id1] = 1;
			cost += w1 + w3;
			q1.pop();
			q3.pop();
			q3.push({a[id1].first - a[id1].second, id1});
		}
	}
	cout << ans << "\n";
	
}

P10736 [SEERC 2020] Archeologists

首先考虑设每个位置的坑的深度为 hi(hi≥0)h_i(h_i\ge 0),此时总价值为:

∑i=1nhigi\sum_{i=1}^{n}h_ig_i

由于 hi−hi−1∈{−1,0,1}h_i-h_{i-1}\in \{-1,0,1\},考虑构建差分数组 di=hi−hi−1d_i=h_i-h_{i-1},此时答案变为:

∑i=1n∑j=1idjgi\sum_{i=1}^{n}\sum_{j=1}^{i}d_jg_i

此时把 ∑j=1idj\sum_{j=1}^{i}d_j 拿出来:

∑j=1ndj∑i=jngi\sum_{j=1}^{n}d_j\sum_{i=j}^{n}g_i

由于 di∈{−1,0,1}d_i\in\{-1,0,1\},所以考虑把 11 对应为左括号,−1-1 对应为右括号,此时这个字符串必须要是一个合法括号序列,最关键的是,我们发现两个配对括号 l,rl,r 之间的差值即为 ∑i=lr−1gi\sum_{i=l}^{r-1}g_i,此时这道题就成功转化为了一个经典的反悔贪心模型了。

正确代码
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 3e5 + 10;
long long n, ans;
priority_queue<int, vector<int>, greater<int> > q;

signed main() {
	
	cin >> n;
	int sum = 0;
	for(int i = 1; i <= n; i++) {
		int x;
		cin >> x;
		q.push(sum);
		sum += x;
		if(sum > q.top()) {
			ans += sum - q.top();
			q.pop();
			q.push(sum);
		}
	}
	cout << ans << "\n";
	
} 

D2

P6821 [PA 2012] Tanie linie

思路略。

错因:

我没有 pop 掉堆顶/fn/fn/fn/fn

思路:

略

  1. 首先正负号相同的一定会缩成一个数
  2. 然后首先把所有正数求和,如果正数的段数大于 mm,则开始反悔:
  3. 如果抛弃一段正数,则两端的负数必定也要抛弃,如果假如选择一段负数,两端的正数也要选择;
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e6 + 10;
struct Node {
	int w, id;
	bool operator < (const Node & x) const {
		return abs(w) > abs(x.w);
	}
};
int n, m, a[N], b[N], cnt1 = 1, cnt2, l[N], r[N], vis[N], ans;
priority_queue<Node> q;

signed main() {

	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> m;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
		if(!a[i]) {
			continue;
		}
		if((b[cnt1] <= 0 && a[i] <= 0) || (b[cnt1] >= 0 && a[i] >= 0)) {
			b[cnt1] += a[i];
		}
		else {
			b[++cnt1] = a[i];
		}
	}
	for(int i = 1; i <= cnt1; i++) {
		l[i] = i - 1;
		r[i] = i + 1;
		ans += max(0ll, b[i]);
		cnt2 += b[i] > 0;
		q.push({b[i], i});
	}
	b[0] = b[cnt1 + 1] = 1e18;
	while(cnt2 > m) {
		while(!q.empty() && vis[q.top().id]) {
			q.pop();
		}
		if(q.empty()) {
			break;
		}
		int i = q.top().id;
		q.pop();
		if(l[i] >= 1 && r[i] <= cnt1 || b[i] > 0) {
			ans -= abs(b[i]);
			b[i] += b[l[i]] + b[r[i]];
			vis[l[i]] = vis[r[i]] = 1;
			l[i] = l[l[i]];
			r[i] = r[r[i]];
			r[l[i]] = i;
			l[r[i]] = i;
			q.push({b[i], i});
			cnt2--;
		}
	}
	cout << ans << "\n";

	return 0;
}

P16289 [蓝桥杯 2026 省 Python A 组] 购电优化

兄弟紫金们我是傻子

思路:

我们虚拟一个电池,这个电池中的电都是预定的,只有用掉时才会结算,此时从左往右跑,如果遇到一个地方需要用电,就从电池里面抽电,直到电的单价不低于在这个点直接购买,然后用这个点的单价往里面塞电,把所有单价大于它的全部弹掉就行了。每个点只会进出队列 O(1)O(1) 次,所以复杂度为 O(n)O(n)。然后也不需要担心会错误,因为这个做法有一个反悔的作用,如果 ii 用了之前的电,并且 j>ij>i 用了 ii 的电,就等价于 jj 用了 ii 前面的电,ii 没有用电池里的电,也就是反悔了。

错因:

在计算用电量和充电量时算错了,导致电的数量错误然后 30pts30pts

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 5e5 + 10;
int n, k, w[N], c[N], cnt;
__int128_t ans;
deque<pair<int, int>> q;

void print(__int128_t ans) {
	if(!ans) {
		return;
	}
	print(ans / 10);
	cout << (int)(ans % 10);
}

signed main() {

	cin >> n >> k;
	for(int i = 1; i <= n; i++) {
		cin >> w[i];
	}
	for(int i = 1; i <= n; i++) {
		cin >> c[i];
	}
	for(int i = 1; i <= n; i++) {
		while(!q.empty() && w[i]) {
			int C = q.front().first, W = q.front().second;
			if(C >= c[i]) {
				break;
			}
			ans += C * min(w[i], W);
			q.pop_front();
			if(w[i] < W) {
				q.push_front({C, W - w[i]});
				cnt -= w[i];
				w[i] = 0;
				break;
			}
			else {
				w[i] -= W;
				cnt -= W;
			}
		}
		ans += w[i] * c[i];
		int now = k - cnt;
		while(!q.empty() && q.back().first >= c[i]) {
			now += q.back().second;
			cnt -= q.back().second;
			q.pop_back();
		}
		if(now) {
			q.push_back({c[i], now});
		}
		cnt += now;
	}
	if(ans) {
		print(ans);	
	}
	else {
		cout << "0\n";
	}

	return 0;
}
原文