跳到正文

题解

题解:P1090 [NOIP 2004 提高组] 合并果子

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

文章信息

发布
2025年2月11日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 2 分钟
题目
P1090

版本

  1. v1当前

思路

考虑贪心策略,每次选择最小的两堆果子合并在一起,直到只剩下一堆,过程中的体力消耗就是答案。
本题可以使用 STL 中的优先队列(priority_queue)求解,也可以手写堆,此处不展示手写堆。

优先队列介绍

dalao 请跳过 dalao 估计是不会来看这篇题解的。

头文件

C++
#include <queue> //使用万能头的人当我没说

定义

大根堆

C++
priority_queue<int> c;  

小根堆

C++
priority_queue<int,vector<int>,greater<int> > c;

插入

C++
c.push();   

访问堆顶元素

C++
c.top();

弹出堆顶元素

C++
c.pop();

判空

C++
c.empty();  

代码

你们最喜欢的代码来了!!!

C++
#include<bits/stdc++.h>
using namespace std;
priority_queue<int,vector<int>,greater<int> > c;
int n,x,y,ans;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>x,c.push(x);
	while(c.size()>1)
	{
		x=c.top();
		c.pop();
		y=c.top();
		c.pop();
		c.push(x+y);
		ans+=x+y;
	}
	cout<<ans;
	return 0;
 } 
原文