餐馆

来源:互联网 发布:vero moda除了淘宝 编辑:程序博客网 时间:2024/04/28 10:01

Question

某餐馆有n张桌子,每张桌子有一个参数:a 可容纳的最大人数; 有m批客人,每批客人有两个参数:b人数,c预计消费金额。 在不允许拼桌的情况下,请实现一个算法选择其中一部分客人,使得总预计消费金额最大

Algorithm

用multiset存取桌子人数信息(不用set因为可能重复),对客人信息排序(先以消费金额排序,如果相同人数少的排前面)。按照消费金额由大到小遍历,找到最合适的桌子(lower_bound函数)。

Accepted Code

#include<iostream>#include<vector>#include<algorithm>#include<set>using namespace std;typedef struct Peo{    int b;    int c;};bool cmp(Peo p1,Peo p2){    if(p1.c == p2.c){        return p1.b > p2.b;    }    return p1.c < p2.c;}int main(){    int n,m;    while(cin>>n>>m){        multiset<int> desk;        for(int i=0;i<n;i++){            int t;            cin>>t;            desk.insert(t);        }        vector<Peo> v(m);        for(int i=0;i<m;i++){            cin>>v[i].b>>v[i].c;        }        sort(v.begin(),v.end(),cmp);        long long res=0;        for(int i=m-1;i>=0;i--){            auto it=desk.lower_bound(v[i].b);            if(it==desk.end())                continue;            else{                res += v[i].c;                desk.erase(it);            }            if(desk.empty())                break;        }        cout<<res<<endl;    }    return 0;}