AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

优先队列的贪心策略

作者: 作者的头像   Ascension ,  2023-05-27 10:07:15 ,  所有人可见 ,  阅读 53


0


1

#include<bits/stdc++.h>
const int N = 1e6 + 10;
int a[N];
signed main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    int n; std::cin >> n;
    for (int i = 1; i <= n; i++) std::cin >> a[i];
    std::priority_queue<int, std::vector<int>, std::greater<int>> q;
    int64_t res = 0;
    for (int i = 1; i <= n; i++) {
        if (q.size() && a[i] > q.top()) {
            res += a[i] - q.top(); q.pop();
            q.push(a[i]); q.push(a[i]);
        }
        else q.push(a[i]);
    }
    std::cout << res << '\n';
    return 0;
}

1 评论


用户头像
Ascension   9天前         踩      回复

自用


你确定删除吗?

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息