zoukankan      html  css  js  c++  java
  • 【每日一题】1. tokitsukaze and Soldier (优先队列 + 排序)

    题目链接:Here

    思路:这道题很容易看出来是考察 优先队列(priority_queue)sort .

    对于容忍人数越高的人来说,团队人数低也更能做到;

    for i = 0 to n - 1:
    	ans = max(ans, vs[i].v + 满足vs[j].s >= vs[i].s 且 i != j 的j中选<= vs[i].s - 1个的vs[j].v的最大和)
    

    所以按 s 降序排序,维护堆即可

    • (mathcal{O}(nlogn))
    #include <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    struct node {
        ll v, s;
    };
    bool cmp(node a, node b) { return a.s > b.s; }
    int main() {
        ios_base::sync_with_stdio(false), cin.tie(0);
        int n;
        cin >> n;
        vector<node> vs(n);
        for (int i = 0; i < n; ++i) {
            cin >> vs[i].v >> vs[i].s;
        }
        // 按 s 大小降序排序
        sort(vs.begin(), vs.end(), cmp);
        ll ans = 0, tmp = 0;
        // 优先队列 q 保存士兵希望团人数的数组,并以战力小为堆顶
        priority_queue<int, vector<int>, greater<int>> q;
        for (int i = 0; i < n; ++i) {
            while (q.size() >= vs[i].s) {
                tmp -= q.top();
                q.pop();
            }
            ans = max(ans, tmp + vs[i].v);
            q.push(vs[i].v), tmp += vs[i].v;
        }
        cout << ans << "
    ";
        return 0;
    }
    

    The desire of his soul is the prophecy of his fate
    你灵魂的欲望,是你命运的先知。

  • 相关阅读:
    mysql复习
    常用函数
    contos7上安装rabbitmq
    linux笔试题
    发布脚本
    Arch最小化安装LXDE桌面环境
    Arch最小化安装X
    Arch安装详解
    Gentoo解决Windows双系统时间不同步的问题
    Gentoo安装详解(五)-- 安装X桌面环境
  • 原文地址:https://www.cnblogs.com/RioTian/p/14619282.html
Copyright © 2011-2022 走看看