跳到正文

个人记录

2.26-小六集训队-407

David_Lu约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年2月26日
收录
2026年2月26日
更新
2026年11月16日
篇幅
约 10 分钟

版本

  1. v2当前
  2. v1

优先队列模板

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
pair <int, int> a;
priority_queue<int> q1;//大根堆 
priority_queue<int, vector<int>, greater<int> > q2;//小根堆 
priority_queue<a> q3;// first 最大更先,其次是second
pair <int, pair <int, int>> b;
priority_queue<b> q3;// first 最大更先,其次是second.first,最后是second.second 
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	push();//入队 
	pop();//出队 
	top();//调取队首 
	size();//获得队伍长度 
	empty();//判断是否为空 
	return 0;
}

P3378 【模板】堆

题意

给一个空数组,有三种操作

  1. 加入一个数到数组里
  2. 输出数组里最小的数
  3. 移除数组里最小的数

思路

只需要使用小根堆(优先队列的一种变形)就行了,因为小根堆会把小的数字自动放到前面

AC代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
const int N = 1e6 + 5;
int h;
priority_queue <int, vector<int>, greater<int> > a;
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	while(n--){
		int op;
		cin >> op;
		if(op == 1){
			int x;
			cin >> x;
			a.push(x);
		}else if(op == 2){
			cout << a.top()<< '\n';
		}else if(op == 3){
			a.pop();
		}
	}
	return 0;
}

P3887 [GDOI2014] 世界杯

题意

中国足球队要组建QQ个阵型,每个阵型有11个守门员AA个后卫,BB个中场,CC个前锋,目前有KK个守门员DD个后卫,MBMB个中场,FF个前锋。每个球队不能有相同的队员,请问每个阵型平均综合水平的最大值,并四舍五入到小数点后22位

思路

这其实不难,只要用到大根堆即可,提取对应数量的后卫,中场,前锋,守门员即可

AC代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int K, D, M, F;
priority_queue <int> k;
priority_queue <int> d;
priority_queue <int> m;
priority_queue <int> f;
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> K>> D>> M>> F;
	for(int i = 1;i <= K;i++){
		int x;
		cin >> x;
		k.push(x);
	}
	for(int i = 1;i <= D;i++){
		int x;
		cin >> x;
		d.push(x);
	}
	for(int i = 1;i <= M;i++){
		int x;
		cin >> x;
		m.push(x);
	}
	for(int i = 1;i <= F;i++){
		int x;
		cin >> x;
		f.push(x);
	}
	int Q;
	cin >> Q;
	while(Q--){
		int A, B, C;
		cin>> A>> B>> C;
		double sum = 0;
		sum += k.top();
		k.pop();
		for(int i = 1;i <= A;i++){
			sum += d.top();
			d.pop();
		}
		for(int i = 1;i <= B;i++){
			sum += m.top();
			m.pop();
		}
		for(int i = 1;i <= C;i++){
			sum += f.top();
			f.pop();
		}
		printf("%.2lf\n", sum / 11);
	}
	return 0;
}

P1334 瑞瑞的木板

题意

有一块巨大的木板,要把它切成nn块,每次切割都会消耗xx个能量单位(xx为切割的木板的长度),请问最少用多少能量单位才可以切好

思路

其实我们可以使用通过往后逆推来计算合并代价,要使用小根堆,每次去除最小的一对元素来合并,知道一直合并到只剩一个元素

AC代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
priority_queue <int, vector<int>, greater<int> > a;
int ans;
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 1;i <= n;i++){
		int x;
		cin >> x;
		a.push(x);
	}
	int sum = 0;
	while(a.size() > 1){
		int a1 = a.top();
		a.pop();
		int a2 = a.top();
		a.pop();
		sum += (a1 + a2);
		a.push(a1 + a2);
	}
	cout << sum;
	return 0;
}

P1878 舞蹈课

题意

有nn个人上舞蹈课,每人都有一个技术值,每次舞蹈技术差距最小的相邻且为异性的一对出列,如果有多对,那就最左边的一对出列

思路

用一个小根堆来存储每个人和旁边异性的人的差值和左边的位置和右边的位置,在准备双向链表和一个数组来记录每个人是否出列,每次判断每个人是否可以和一旁的异性出列跳舞即可

AC代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
int n;
const int N = 2e5 + 5;
int a[N];
string s;
priority_queue <PIII, vector<PIII>, greater<PIII> > q;
int is[N], pre[N], nxt[N];
int ans1[N], ans2[N];
int cnt;
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	cin >> s;
	for(int i = 1;i <= n;i++){
		cin >> a[i];
	}
	s = " " + s;
	for(int i = 2;i <= n;i++){
		if(s[i - 1] != s[i]){
			q.push({abs(a[i] - a[i - 1]), {i-1, i}});
		}
	}
	for(int i = 1;i <= n;i++){
		pre[i] = i - 1;
		nxt[i] = i + 1;
	}
	is[0] = is[n+1] = 1;
	while(q.empty() == 0){
		PIII t = q.top();
		q.pop();
		int x = t.second.first;
		int y = t.second.second;
		if(is[x] == 0 and 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 and is[nxt[y]] == 0){
				if(s[pre[x]] != s[nxt[y]]){
					q.push({{abs(a[pre[x]] - a[nxt[y]])}, {pre[x], nxt[y]}});
				}
			}
		}
	}
	cout << cnt<< '\n';
	for(int i = 1;i <= cnt;i++){
		cout << ans1[i]<< ' '<< ans2[i]<< '\n';
	}
	return 0;
}

P9833 [USACO05OPEN] Expedition G

题意

奶牛们要前往城市,但邮箱坏了,每走一公里就会漏一升油,到城市还有ll个单位时还有pp个升的油,但是有nn个加油站,每个加油站距离城市dd公里,可以加ff升油,请问奶牛们加几次油就可以到达城市,或者永远无法到达输出−1-1

思路

用一个大根堆记录加油站能加的油量,一直往前走,油不够了就取之前加油站没加过且加油最多的油量

AC代码

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e4 + 5;
int n;
vector <pair <int, int> > tmp, s;
priority_queue<int> a;
signed main(){
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 1;i <= n;i++){
		int d, f;
		cin >> d>> f;
		tmp.push_back({d, f});
	}
	int l, p;
	cin >> l>> p;
	for(auto &i : tmp){
		int d = i.first, f = i.second;
		s.push_back({l - d, f});
	}
	s.push_back({l, 0});
	sort(s.begin(), s.end());
	int pos = 0;
	int you = p;
	int ans = 0;
	for(auto &i : s){
		int d = i.first - pos;
		while(you < d){
			if(a.empty()){
				cout << -1;
				return 0;
			}
			you += a.top();
			a.pop();
			ans++;
		}
		you -= d;
		pos = i.first;
		if(i.first == l){
			break;
		}
		a.push(i.second);
	}
	cout << ans;
	return 0;
}
原文