蓝桥杯(C++ 最大开支 优先队列)

优先队列: 蓝桥杯(C++ 整数删除 优先队列 )-CSDN博客

思路:

1、每个人依此选择项目,每个人选项目时都(选择当下花费增加最多的项目),若项目i的门票价格为kx+b,那么增加一个人选择的花费为:increase =(k*(x+1)+b)*(x+1)- ((k*x)+b)*x x = k(2x+1) + b

2、可以用优先队列存项目的k、b、x(选择这个项目的人数)、increase(当前项目人数增加为x+1所增加的花费),使increase最大的项目排在队首,每加一个人出队一个并使总花费增加increase,即总花费money = money + increase

3、更新x和increase,若increase > 0重新入队,若increase <= 0不重新入队(人数到了开口向下的二次函数的对称轴,花费开始下降,不能再增加人数了

代码:

cpp 复制代码
#include<iostream>
#include<queue>
#include<functional>
using namespace std;
using ll = long long;
struct node
{
    int k, b, x , increase;
};
bool operator < (const struct node a, const struct node b)//重载<
{
    return a.increase < b.increase;
}
int main()
{
    int n, m;
    cin >> n >> m;
    priority_queue<node>h;//大根堆
    node a;
    for (int i = 0; i < m; i++)
    {
        cin >> a.k >> a.b;
        a.increase = a.k + a.b;
        if (a.increase <= 0)//小于等于零的不入队
            continue;
        a.x = 1;
        h.push(a);
    }
    ll money = 0, person;
    for (person = 0; !h.empty() && person < n; person++)
    {
        if (h.top().increase > 0)//防止第一个就小于零
        {
            node temp = h.top();
            h.pop();
            money += temp.increase;//加上增加的花费
            //increase=(k*(x+1)+b)*(x+1)-((k*x)+b)*x 再增加一个人会不会比之前花费更多
            temp.increase = temp.k * (2 * temp.x + 1) + temp.b;
            if (temp.increase > 0)//比之前还更多,重新入队
            {
                temp.x += 1;
                h.push(temp);
            }
        }
        else
            break;
    }
    cout << money;
}
相关推荐
雾岛听蓝5 分钟前
Qt开发核心笔记:从HelloWorld到对象树内存管理与坐标体系详解
开发语言·经验分享·笔记·qt
無限進步D4 小时前
Java 运行原理
java·开发语言·入门
是苏浙4 小时前
JDK17新增特性
java·开发语言
阿里加多7 小时前
第 4 章:Go 线程模型——GMP 深度解析
java·开发语言·后端·golang
likerhood8 小时前
java中`==`和`.equals()`区别
java·开发语言·python
IronMurphy8 小时前
【算法三十九】994. 腐烂的橘子
算法
zs宝来了8 小时前
AQS详解
java·开发语言·jvm
Ares-Wang9 小时前
算法》》旅行商问题 TSP、7座桥问题 哈密顿回路 深度优先 和 宽度优先
算法·深度优先·宽度优先
Liqiuyue9 小时前
Transformer:现代AI革命背后的核心模型
人工智能·算法·机器学习
WolfGang0073219 小时前
代码随想录算法训练营 Day34 | 动态规划 part07
算法·动态规划