MT3030 天梯赛

跟MT3029战神小码哥类似,都是贪心+堆。注意开long long

这里的堆顶为战斗力最小的,便于贪心的反悔操作。先按容忍度从大到小排序(q中总容忍度取决于最小的容忍度),再向q中存数,存到不能容忍之后再把堆顶踢出,取最大值。

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e5 + 10;
long long int n, ans, sum;
struct node
{
    long long int a;
    long long int b;
    bool operator>(const node &b) const
    {
        return a > b.a;
    }
} a[N];
priority_queue<node, vector<node>, greater<node>> q; // 战斗力从小到大排列
bool cmp(node a, node b)
{ // 按b容忍度从大到小排序
    return a.b > b.b;
}
int main()
{
    cin >> n;
    for (long long int i = 1; i <= n; i++)
    {
        cin >> a[i].a >> a[i].b;
    }
     sort(a + 1, a + n + 1, cmp); // 按容忍度从大到小排列
    for (long long int i = 1; i <= n; i++)
    {
        q.push(a[i]);
        sum += a[i].a; // 存战斗力
        while (q.size() > a[i].b)
        {
            sum -= q.top().a; // 取出q.top()战斗力最小的
            q.pop();
        }
        ans = max(ans, sum);
    }
    cout << ans;
    return 0;
}
相关推荐
励志的小陈3 小时前
数据结构--二叉树知识讲解
数据结构
自信150413057594 小时前
重生之从0开始学习c++之模板初级
c++·学习
leobertlan4 小时前
好玩系列:用20元实现快乐保存器
android·人工智能·算法
青梅橘子皮4 小时前
C语言---指针的应用以及一些面试题
c语言·开发语言·算法
笨笨饿4 小时前
#58_万能函数的构造方法:ReLU函数
数据结构·人工智能·stm32·单片机·硬件工程·学习方法
历程里程碑4 小时前
2. Git版本回退全攻略:轻松掌握代码时光机
大数据·c++·git·elasticsearch·搜索引擎·github·全文检索
极客智造4 小时前
深度解析 C++ 类继承与多态:面向对象编程的核心
c++
_深海凉_5 小时前
LeetCode热题100-有效的括号
linux·算法·leetcode
零号全栈寒江独钓7 小时前
基于c/c++实现linux/windows跨平台获取ntp网络时间戳
linux·c语言·c++·windows
CSCN新手听安7 小时前
【linux】高级IO,以ET模式运行的epoll版本的TCP服务器实现reactor反应堆
linux·运维·服务器·c++·高级io·epoll·reactor反应堆