LC 1383. Maximum Performance of a Team

link

 

 

class Solution {
public:
    #define LL long long
    int maxPerformance(int n, vector<int>& speed, vector<int>& efficiency, int k) {
        vector<pair<int,int>> spef;
        priority_queue<int, vector<int>, greater<int>> pq;
        for(int i=0;i<n;i++){
            spef.push_back({efficiency[i],speed[i]});
        }
        sort(spef.begin(), spef.end());
        LL res=0;
        LL sum=0;
        for(int i=n-1;i>=n-k;i--){
            sum+=spef[i].second;
            pq.push(spef[i].second);
            res=max(res,sum*spef[i].first);
        }
        for(int i=n-k-1;i>=0;i--){
            if(spef[i].second>pq.top()){
                sum-=pq.top();
                sum+=spef[i].second;
                pq.pop();
                pq.push(spef[i].second);
                res=max(res,sum*spef[i].first);
            }
        }
        
        int mod=1E9+7;
        return (int)(res%mod);
    }
};

 

posted @ 2020-03-15 17:55  feibilun  阅读(240)  评论(0编辑  收藏  举报