b_nk_做项目的最大收益问题(大小根堆)

w是初始资金、选k个项目,一行项目的所需的启动资金,一行项目利益,问该如何选择做项目,能让你最终的收益最大?

思路:贪心选即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
ll n,w,k;
struct node {
    int c,p;
}A[N];
struct cmp1{
    bool operator()(node& a, node& b) {
        return a.c > b.c;
    }
};
struct cmp2{
    bool operator()(node& a, node& b) {
        return a.p < b.p;
    }
};
int main() {
    std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin>>n>>w>>k; 
    for (int i=0; i<n; i++) cin>>A[i].c;
    for (int i=0; i<n; i++) cin>>A[i].p;
    priority_queue<node, vector<node>, cmp1> minCostQ;
    priority_queue<node, vector<node>, cmp2> maxProfQ;
    for (int i=0; i<n; i++) minCostQ.push(A[i]);

    for (int i=0; i<k; i++) {
        while (!minCostQ.empty() && minCostQ.top().c<=w) {
            maxProfQ.push(minCostQ.top()), minCostQ.pop();
        }
        w+=maxProfQ.top().p;
        maxProfQ.pop();
    }
    cout<<w;
    return 0;
}
posted @ 2021-02-20 17:31  童年の波鞋  阅读(124)  评论(0编辑  收藏  举报