跳到正文

个人记录

2026.2.26代码量集训班比赛复盘+总结(贪心)

Hych2025xx约 16 分钟
原文
文章信息与版本

文章信息

发布
2026年2月26日
收录
2026年2月28日
更新
2026年11月13日
篇幅
约 16 分钟
获赞
1

版本

  1. v2当前
  2. v1

优先队列用法(以便复习)

C++
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int N = 5e6 + 5;
typedef pair<int, int> PI;
typedef pair<int, PI> PII;
//priority_queue<int> pq; //默认大根堆
//priority_queue<int, vector<int>, greater<int> > pq;//小根堆
//priority_queue<PI> pq;//first大的优先,first相同再second大的优先 
//first大的优先,再second.first大的优先, 再second.second大的优先
priority_queue<PII> pq;
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	pq.push({1, {2, 3}});
	pq.push({1, {2, 4}});
	PII f = qp.top();
	cout << f.first << " " << f.second.first << " " << f.second.second << '\n';
	if(!q.empty() || q.size() > 0)qp.pop();
	return 0;
} 

T1 P3378 【模板】堆

题意分析

题目传送门:https://www.luogu.com.cn/problem/P3378

给定一个数列,初始为空,请支持下面三种操作:

1.给定一个整数x,请将x加入到数列中。

2.输出数列中最小的数。

3.删除数列中最小的数(如果有多个数最小,只删除1个)。

赛时思路

这道题可以用优先队列(堆)来做。因为它要输出最小的元素,所以我们在这里定义一个小根堆priority_queue<int, vector<int>, greater<int> > q;这样,队列内会按照从小到大的顺序自动排序(堆顶就是最小的元素),然后我们再按照题目的要求进行三种操作即可过此题(注意,每次输出和删除是要进行判空的,不然有可能爆0)。

比赛用时

用了差不多五分钟的时间就搞完了基础的代码和运行。

正确代码

C++
#include<bits/stdc++.h>
#define endl '\n' 
#define int long long
using namespace std;
priority_queue<int, vector<int>, greater<int> > q;//小根堆,注意要有空格,不然一些程序会报错
int n, op, x;//分别是操作次数,操作类型和数
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> op;
		if(op == 1){
			cin >> x;
			q.push(x);//入队
		}
		if(op == 2){
			if(!q.empty()) cout << q.top() << '\n';//判空加输出堆顶
		}
		if(op == 3){
			if(!q.empty()) q.pop();//判空加出队
		}
	}
	return 0;
}

错因

没有错误。

T2 P3887 [GDOI2014] 世界杯

题意分析

题目传送门:https://www.luogu.com.cn/problem/P3887

输入每个守门员,后卫,中场,前锋的实力,按照输入的每个阵型选出队员并求出每个阵型最大的平均实力值(第一个阵型选了的人第二个阵型就不能选了)。

赛时思路

还是一样的用大根堆(因为是最大值)priority_queue<int> k1, d1, m1, f1;来记录每个人的实力,再用一个sum来记录实力的和。按照每个阵型所需这种队员的数量依次判空,和增加,出队(不要忘记最后加一个守门员!!!),然后和除以11,四舍五入到小数点后第二位输出即可。

比赛用时

用了差不多二三十分钟的时间就搞完了基础的代码和运行(四舍五入的代码忘记加.了)。

正确代码

C++
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int k, d, m, f, q, a, b, c,x;
priority_queue<int> k1, d1, m1, f1;//大根堆(分别是守门员、后卫、中场、前锋)
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++) cin >> x, k1.push(x);//输入守门员
	for(int i = 1; i <= d; i++) cin >> x, d1.push(x);//输入后卫
	for(int i = 1; i <= m; i++) cin >> x, m1.push(x);//输入中场
	for(int i = 1; i <= f; i++) cin >> x, f1.push(x);//输入前锋
	cin >> q;
	for(int i = 1; i <= q; i++){
		double sum = 0.0;//和
		cin >> a >> b >> c;
		for(int j = 1; j <= a; j++){
			if(!d1.empty()) sum += d1.top(), d1.pop();//后卫(判空、加和、出队)
		}
		for(int j = 1; j <= b; j++){
			if(!m1.empty()) sum += m1.top(), m1.pop();//中场(判空、加和、出队)
		}
		for(int j = 1; j <= c; j++){
			if(!f1.empty()) sum += f1.top(), f1.pop();//前锋(判空、加和、出队)
		}
		sum += k1.top(), k1.pop();//守门员(判空、加和、出队)
		sum /= 11;//计算平均值
		printf("%.2lf\n", sum);//四舍五入到小数点后第`2`位并输出
	}
	return 0;
}

错因

没有错误。

T3 P1334 瑞瑞的木板

题意分析

题目传送门:https://www.luogu.com.cn/problem/P1334

瑞瑞测量栅栏并发现他需要n根木板,每根的长度为整数l[i]。于是,他买了一根足够长的木板,长度为所需的n根木板的长度的总和,他决定将这根木板切成所需的n根木板(瑞瑞在切割木板时不会产生木屑,不需考虑切割时损耗的长度)。

瑞瑞切割木板时使用的是一种特殊的方式,这种方式在将一根长度为x的木板切为两根时,需要消耗x个单位的能量。瑞瑞拥有无尽的能量,但现在提倡节约能量,所以作为榜样,他决定尽可能节约能量。显然,总共需要切割(n−1)次,问题是,每次应该怎么切呢?请编程计算最少需要消耗的能量总和。

赛时思路

我被这道题的输入输出样例给坑了!!!!!

我的思路是用一个大根堆来记录需要木板的长度,然后用这些长度的总和依次去减所需要的长度并加上相应的能量,然后出队(中间特判一下剩下的木板如果等于需要的木板就不需要切割了)。

比赛用时

用了差不多十分钟的时间搞完了基础的代码和运行。

正解思路

反向的合并果子,每次取最小值和次小值,然后出队(判空),接着累加合并代价,合并后的木板再次入队。

正确代码

C++
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int n, l, ans, a, b, sum;
priority_queue<int, vector<int>, greater<int> > qp;//小根堆
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> l, qp.push(l);//入队
    while(qp.size() > 1){//另一种判空
        a = qp.top(), qp.pop();//最小值
        b = qp.top(), qp.pop();//次小值
        sum = a + b;
        ans += sum;//累加合并所需要的能量
        qp.push(sum);//合并后的木板再次入队
    }
	cout << ans << '\n';//完美输出
	return 0; 
}

错因

10分,我被这道题的输入输出样例给坑了!!!!!

因为我写的代码非常符合样例的数据,所以我认为我对了,实则没有。

T4 P1878 舞蹈课

题意分析

题目传送门:https://www.luogu.com.cn/problem/P1878

有n个人参加一个舞蹈课。每个人的舞蹈技术由整数来决定。在舞蹈课的开始,他们从左到右站成一排。当这一排中至少有一对相邻的异性时,舞蹈技术相差最小的那一对会出列并开始跳舞。如果不止一对,那么最左边的那一对出列。一对异性出列之后,队伍中的空白按原顺序补上(即:若队伍为ABCD,那么BC出列之后队伍变为AD)。舞蹈技术相差最小即是a[i]的绝对值最小。

任务是模拟以上过程,确定跳舞的配对及顺序。

赛时思路

还是时间问题,于是就直接打了个非常没有存在感的表,果然0分。

比赛用时

无。

正解思路

使用双向链表,思路可以看着代码的注释理解。

正确代码

C++
//贪心、优先队列和链表
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;//pair数组
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;
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
    cin >> n >> 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 << '\n';//输出答案
    for(int i = 1; i <= cnt; i++) cout << ans1[i] << " " << ans2[i] << '\n';
    return 0;
}

错因

0分,打表失败。

T5 P9833 [USACO05OPEN] Expedition G

题意分析

题目传送门:https://www.luogu.com.cn/problem/P9833

一群奶牛抢了一辆卡车决定前往树林里探险,但是由于它们的驾驶技术太糟,油箱在路上给弄破了,所以它们每前进一个单位的路程就会漏掉一个单位的油。

为了修好油箱,奶牛们必须前往最近的城市(不会超过1e6单位路程)。在当前位置和城市之间有n个加油站.奶牛可以在加油站加1到100单位的油。

对于人来说,树林是个危险的地方;对奶牛来说,更是这样。所以,奶牛要尽可能的少停站加油,幸运的是,这辆卡车的油箱非常大,你可以认为它的容量是无穷大的。 卡车在离城l个单位时还有p个单位的油,你要算出奶牛们至少要停几站才能到城市,或者奶牛们根本到不了城市。

赛时思路

没时间了只能打表。

比赛用时

无。

正解思路

将加油站按离起点的距离排序,用大根堆记录经过的加油站的油量。一直往前走,油不够时就取之前最大的油站加油,保证加油次数最少,直到能到终点或无法到达。

重点:

输入的加油站是离城市的距离,要先读入l再转换成离起点的距离( l - d ),并按这个距离从小到大排序。把终点(城市)也当成一个距离为l、油量为0的“虚拟加油站”,便统一处理。大根堆存经过的油站油量,每次缺油时取最大的,确保加油次数最少。

正确代码

C++
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 1e4 + 10;
vector<pair<int, int>> temp, stations; 
priority_queue<int> pq;
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        int d, f;
        cin >> d >> f;
        temp.push_back({d, f});
    }
    int l, p; 
    cin >> l >> p;
    //转换成离起点的距离,并加入终点   
    for(auto &t : temp){  
        int d = t.first, f = t.second; 
        stations.push_back({l - d, f});
    }
    stations.push_back({l, 0}); //终点虚拟站点
    sort(stations.begin(), stations.end());  
    int pos = 0, you = p, ans = 0;  
    for(auto &s : stations){
        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;  
        if(s.first == l) break;//成功到达终点
        pq.push(s.second);
    }
    cout << ans << '\n';//输出答案
	return 0;
}

错因

20分,打表成功!(输出-1)。

原文