蓝桥杯2024年第十五届省赛真题-爬山 知识点:贪心+优先队列

217 阅读1分钟

www.dotcpp.com/oj/problem3…

每次贪心的处理最高的山峰,把高山峰变矮,这样爬山花费的力气最小。另外,处理完最高的山峰之后还要再把处理过的山峰继续加入堆中,这样如果处理过的山峰仍然是最高的山峰的那么下一次还是会处理它。

code

h[i]最大1e5,相加会爆int,要开long long

#include<bits/stdc++.h>
#include<queue>
using namespace std;
#define endl '\n'
#define int long  long
signed main()

{
	cin.tie(nullptr)->sync_with_stdio(false);


	int n, p, q; cin >> n >> p >> q;

	priority_queue<int, vector<int>>heap;
	for (int i = 0; i < n; i++)
	{
		int x;
		cin >> x;
	    heap.push(x);
	}

	//p次根号操作
	while(p--)
	{
		int val = heap.top();
		heap.pop();
		val = sqrt(val);
		heap.push(val);
	}

	//q次除半操作
	while (q--)
	{
		int val = heap.top();
		heap.pop();
		val = val / 2;
		heap.push(val);
	}


	//累加结果
	int ans = 0;
	while (!heap.empty())
	{
		ans += heap.top();
		heap.pop();
	}
	
	cout << ans;

	return 0;
}

image.png