C2. Powering the Hero (hard version)(优先队列板子)

题目链接:Problem - C2 - Codeforces

题目:

This is a hard version of the problem. It differs from the easy one only by constraints on $n$ and $t$.

There is a deck of $n$ cards, each of which is characterized by its power. There are two types of cards:

  a hero card, the power of such a card is always equal to $0$;  a bonus card, the power of such a card is always positive. You can do the following with the deck:

  take a card from the top of the deck;  if this card is a bonus card, you can put it on top of your bonus deck or discard;  if this card is a hero card, then the power of the top card from your bonus deck is added to his power (if it is not empty), after that the hero is added to your army, and the used bonus discards. Your task is to use such actions to gather an army with the maximum possible total power.

输入:

The first line of input data contains single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the test.

The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of cards in the deck.

The second line of each test case contains $n$ integers $s_1, s_2, \dots, s_n$ ($0 \le s_i \le 10^9$) — card powers in top-down order.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

输出:Output $t$ numbers, each of which is the answer to the corresponding test case — the maximum possible total power of the army that can be achieved.


思路:我们通过题意可以知道如果摸到奖励卡就要选择的放到顶端或弃掉,遇到英雄卡,那他将继承牌堆顶的那张牌的力量,因此需要规划好那些排放到牌堆上,因此可以通过使用优先队列的形式,将摸到英雄牌前的奖励卡,放到队列中自动排序。每次取队头,最后没取的牌就是弃牌,取了的则是英雄力量。

代码:

#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
#define pb push_back
const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
using namespace std;
typedef pair<int,int>pii;
int n, m,k,sum,ans=INF;
map<char, int>mp;        
int a[N],b[N];
string s;
vector<pii>g[N],vg;
int  dist[N];bool vis[N],st[N];
 priority_queue <int,vector<int>,less<int> >q;
void solve () {
   scanf("%lld\n",&n);
  while(!q.empty())   {  q.pop(); }
   for(int i=1;i<=n;i++){
    scanf("%lld",&a[i]);
   }
   int cnt=0,sum=0;
    for(int i=1;i<=n;i++){
        if(a[i]!=0){
            q.push(a[i]);
        }
        else{
            if(q.size()){
            sum+=q.top();
            q.pop();}
        }
    }
    cout<<sum<<endl;
}
signed main () {
    int T = 1; 
    std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    //  T = read ();
    scanf("%lld",&T);
    while (T --) solve ();
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
03-05
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值