• 2133排队接水——优先队列


    n个人一起排队接水,第i个人需要a[i]的时间来接水。
    1 <= n <= 1000
    1 <= a[i] <= 1000
    同时只能有一个人接水,正在接水的人和没有接水的人都需要等待。
    完成接水的人会立刻消失,不会继续等待。
    你可以决定所有人接水的顺序,并希望最小化所有人等待时间的总和。
    Input
    第一行一个整数n
    接下来n行,每行一个整数表示a[i]
    Output
    一行一个整数,表示所有人等待时间的总和的最小值
    Input示例
    3
    1
    2
    3
    Output示例
    10
    思路:优先队列的基础使用
    #include<cstdio>
    #include<queue>
    #include<vector>
    #include<iostream>
    #include<functional>
    #include<algorithm>
    using namespace std;
    
    int main()
    {
        int n;
        cin >> n;
        priority_queue<int,vector<int>,greater<int>>que;
        for (int i = 0; i < n; i++){
            int temp; cin >> temp;
            que.push(temp);
        }
        int sum = 0;
        while (!que.empty()){
            //cout<<que.top()<<" ";
            sum += que.top()*que.size();
            que.pop();
        }
        cout << sum << endl;
        return 0;
    }
     
  • 相关阅读:
    腾讯云 ubuntu 上tomcat加载项目很慢
    ubuntu 中iptables
    linux ssh修改 默认22端口
    jetty 客服端 与服务端
    spring mvc 拦截器
    linux配置iptables(3)
    el取bean 对象属性规则
    mybatis 反射bean规则
    fastjson tojson部分规则
    正则获取参数 分组
  • 原文地址:https://www.cnblogs.com/zengguoqiang/p/9145363.html
Copyright © 2020-2023  润新知