C2. Powering the Hero (hard version)

题目:样例:

输入
5
5
3 3 3 0 0
6
0 3 3 0 0 3
7
1 2 3 0 4 5 0
7
1 2 5 0 4 3 0
5
3 1 0 0 4

输出
6
6
8
9
4

思路:

        贪心思维题,这道题要求的是一张一张的凑卡牌,凑到的是力量赋值卡就存储好,抽到 0 就是英雄卡,当我们存储中有力量赋值卡,就将该力量赋值给该英雄,并获得该英雄的战力,求最佳操作获得的最大战力总和是多少。

        我们将每次抽到的力量赋值卡存储的时候,遇到英雄卡,就赋值给最大存储即可,而赋值卡值最小的放到后面,倘若遇到了英雄卡,也会将该最小力量赋值卡赋值给英雄的,倘若没遇到,相当于弃用掉。

        所以我们用个优先队列存储并赋值即可。

代码详解如下:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <unordered_map>
#define endl '\n'
#define int long long
#define YES puts("YES")
#define NO puts("NO")
#define umap unordered_map
#define All(x) (x).begin(),(x).end()
#pragma GCC optimize(3,"Ofast","inline")
#define ___G std::ios::sync_with_stdio(false),cin.tie(0), cout.tie(0)
using namespace std;
const int N = 2e6 + 10;

inline void solve()
{
	int n,ans = 0;
	cin >> n;
	
	// 存储力量赋值卡,优先赋值的最大战力卡
	priority_queue<int>q;
	
	// 开始抽卡
	while(n--)
	{
		int x;
		cin >> x;
		
		// 如果是力量赋值卡,存储好赋值卡
		// 如果是英雄,判断是否有赋值卡,有则获得该最佳战力
		if(x) q.push(x);
		else if(q.size()) ans += q.top(),q.pop();
	}
	// 输出最佳战力总和
	cout << ans << endl;
}

signed main()
{
//	freopen("a.txt", "r", stdin);
	___G;
	int _t = 1;
	cin >> _t;
	while (_t--)
	{
		solve();
	}
	return 0;
}

最后提交:

猜你喜欢

转载自blog.csdn.net/hacker_51/article/details/133417761
今日推荐