跳到正文

个人记录

3.21 周六下午--贪心训练1--优先队列进阶

WUzongze约 14 分钟
原文
文章信息与版本

文章信息

发布
2026年3月21日
收录
2026年3月21日
更新
2026年11月13日
篇幅
约 14 分钟

版本

  1. v2当前
  2. v1

比赛信息

比赛名称:3.21 周六下午—贪心训练1—优先队列进阶

时间:2026.3.21 周六下午

链接:3.21 周六下午—贪心训练1—优先队列进阶

题面下载:贪心训练1 题面

题解下载:贪心训练1 题解

优先队列

基础操作

创建

  • 大根堆创建 priority_queue<int> q;
  • 小根堆创建 priority_queue<int, vector<int>, greater<int> > q;

出入队

  • 入队 q.push(x);
  • 根节点出队q.pop();

读取

  • 读取根节点 q.top()
  • 读取节点数量q.size()

多参数

双参数

  • 创建
C++
typedef pair<int, int> PII;
priority_queue<PII> q;
PII f = q.top();
  • 读取
代码
first大的优先,first相同再 second大的优先
   f.first                   f.second
  • 入队 q.push({a,b});

三参数

  • 创建
C++
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
priority_queue<PIII> q;
PIII f = q.top();
  • 读取
代码
first大的优先, 再是second.first优先,再是second.second
   f.first       f.second.first       f.second.second
  • 入队 q.push({a,{b,c}}});

更多参数以此类推

题目

【模板】堆

题号:P3378

链接:【模板】堆

代码:

C++
#include <bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;
int main(){
	int n;
	cin>>n;
	for (int i = 1;i <= n;i++){
		int op;
		cin>>op;
		if (op == 1){
			int x;
			cin>>x;
			q.push(x);
		}
		else if (op == 2){
			cout<<q.top()<<endl;
		}
		else{
			q.pop();
		}
	}
	return 0;
}

[GDOI2014] 世界杯

题号:P3887

链接:[GDOI2014] 世界杯

思路:

T2

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	priority_queue<int> pq_k, pq_d, pq_m, pq_f;
	int K, D, M, F;
	cin >> K >> D >> M >> F;
	for (int i = 0; i < K; i++) {
		int x; cin >> x;
		pq_k.push(x);
	}
	for (int i = 0; i < D; i++) {
		int x; cin >> x;
		pq_d.push(x);
	}
	for (int i = 0; i < M; i++) {
		int x; cin >> x;
		pq_m.push(x);
	}
	for (int i = 0; i < F; i++) {
		int x; cin >> x;
		pq_f.push(x);
	}
	int Q;
	cin >> Q;
	while (Q--) {
		int A, B, C;
		cin >> A >> B >> C;
		ll total = 0;
		total += pq_k.top();
		pq_k.pop();
		for (int i = 0; i < A; i++) {
			total += pq_d.top();
			pq_d.pop();
		}
		for (int i = 0; i < B; i++) {
			total += pq_m.top();
			pq_m.pop();
		}
		for (int i = 0; i < C; i++) {
			total += pq_f.top();
			pq_f.pop();
		}
		double avg = total / 11.0;
		cout << fixed << setprecision(2) << avg << endl;
	}
	return 0;
}

瑞瑞的木板

题号:P1334

链接:瑞瑞的木板

思路:

T3

代码:

C++
#include <bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int>, greater<int> > heap;
long long ans = 0;
int a,b,sum;
int main(){
	int n;
	cin>>n;
	for (int i = 1;i <= n;i++){
		int li;
		cin>>li;
		heap.push(li);
	}
	while (heap.size() > 1){
		a = heap.top(); heap.pop();
		b = heap.top(); heap.pop();
		sum = a + b;
		ans += sum;
		heap.push(sum);
	}
	cout<<ans;
	return 0;
}

舞蹈课

题号:P1878

链接:舞蹈课

思路:

T4

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
const int N = 2e5 + 10;
priority_queue<PIII, vector<PIII>, greater<PIII> > q;
int n, a[N], is[N], ans1[N], ans2[N], nxt[N], pre[N];
int cnt = 0;
string s;
int main()
{
	cin >> n;
	cin >> s;
	for(int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	for(int i = 1; i < n; i++)
	{
		if(s[i-1] != s[i])
		{
			q.push({abs(a[i] - a[i+1]), {i, i+1}});
		}
	}
	for(int i = 1; i <= n; i++)
	{
		nxt[i] = i + 1;
		pre[i] = i - 1;
	}
	while(q.empty() == 0)
	{
		PIII t = q.top();
		q.pop();
		int x = t.second.first, y = t.second.second;
		if(is[x] == 0 && is[y] == 0)
		{
			nxt[pre[x]] = nxt[y];
			pre[nxt[y]] = pre[x];
			cnt++;
			ans1[cnt] = x, ans2[cnt] = y;
			is[x] = 1, is[y] = 1;
			if(is[pre[x]] == 0 && is[nxt[y]] == 0)
			{
				if((s[pre[x]-1] + s[nxt[y]-1]) == ('G' + 'B'))
				{
					q.push({abs(a[pre[x]] - a[nxt[y]]), {pre[x], nxt[y]}});
				}
			}
		}
	}
	cout << cnt << endl;
	for(int i = 1; i <= cnt; i++)
	{
		cout << ans1[i] << " " << ans2[i] << endl;
	}
	return 0;
}

[USACO05OPEN] Expedition G

题号:P9833

链接:[USACO05OPEN] Expedition G

思路:

T5

代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 1e4 + 10;
typedef pair<int, int> PII;
PII temp[N], stations[N];
priority_queue<int> pq;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		int d, f;
		cin >> d >> f;
		temp[i] = {d, f};
	}
	int l, p;
	cin >> l >> p;
	for(int i = 1; i <= n; i++)
	{
		int d = temp[i].first, f = temp[i].second;
		stations[i] = {l - d, f};
	}
	stations[n + 1] = {l, 0};
	sort(stations + 1, stations + n + 2);
	int pos = 0, you = p, ans = 0;
	for(int i = 1; i <= n + 1; i++)
	{
		PII s = stations[i];
		int dist = s.first - pos;
		while(you < dist)
		{
			if(pq.empty())
			{
				cout << -1 << endl;
				return 0;
			}
			you += pq.top();
			pq.pop();
			ans++;
		}
		you -= dist;
		pos = s.first;
		pq.push(s.second);
	}
	cout << ans << endl;
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
const int N = 2e5 + 10;
priority_queue<PIII, vector<PIII>, greater<PIII> > q;
int n, a[N], is[N], ans1[N], ans2[N], nxt[N], pre[N];
int cnt = 0;
string s;
int main()
{
	cin >> n;
	cin >> s;
	for(int i = 1; i <= n; i++)
	{
		cin >> a[i];
	}
	for(int i = 1; i < n; i++)
	{
		if(s[i-1] != s[i])
		{
			q.push({abs(a[i] - a[i+1]), {i, i+1}});
		}
	}
	for(int i = 1; i <= n; i++)
	{
		nxt[i] = i + 1;
		pre[i] = i - 1;
	}
	while(q.empty() == 0)
	{
		PIII t = q.top();
		q.pop();
		int x = t.second.first, y = t.second.second;
		if(is[x] == 0 && is[y] == 0)
		{
			nxt[pre[x]] = nxt[y];
			pre[nxt[y]] = pre[x];
			cnt++;
			ans1[cnt] = x, ans2[cnt] = y;
			is[x] = 1, is[y] = 1;
			if(is[pre[x]] == 0 && is[nxt[y]] == 0)
			{
				if((s[pre[x]-1] + s[nxt[y]-1]) == ('G' + 'B'))
				{
					q.push({abs(a[pre[x]] - a[nxt[y]]), {pre[x], nxt[y]}});
				}
			}
		}
	}
	cout << cnt << endl;
	for(int i = 1; i <= cnt; i++)
	{
		cout << ans1[i] << " " << ans2[i] << endl;
	}
	return 0;
}
原文