• 【每日一题】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
    你灵魂的欲望,是你命运的先知。

  • 相关阅读:
    ORA-14404
    ORA-00845
    ORA-00054
    oracle-11g-配置dataguard
    ORACLE 11G 配置DG 报ORA-10458、ORA-01152、ORA-01110
    Python:if __name__ == '__main__'
    HDFS-Shell 文件操作
    HDFS 概述
    PL/SQL Developer
    CentOS7 图形化方式安装 Oracle 18c 单实例
  • 原文地址:https://www.cnblogs.com/RioTian/p/14619282.html
Copyright © 2020-2023  润新知