分享

餐馆,自己动手case通过率为50.00%

 木俊 2018-10-08

题目描述

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

输入描述:

输入包括m+2行。 第一行两个整数n(1 <= n <= 50000),m(1 <= m <= 50000) 第二行为n个参数a,即每个桌子可容纳的最大人数,以空格分隔,范围均在32位int范围内。 接下来m行,每行两个参数b,c。分别表示第i批客人的人数和预计消费金额,以空格分隔,范围均在32位int范围内。

输出描述:

输出一个整数,表示最大的总预计消费金额
示例1

输入

复制
3 5 2 4 2 1 3 3 5 3 7 5 9 1 10

输出

复制
20
///////////////////////
链接:https://www./questionTerminal/d2cced737eb54a3aa550f53bb3cc19d0
来源:牛客网

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
 
struct guest {
    int num;
    int money;
};
bool cmp(guest a, guest b) {
    if (a.money == b.money) {
        return a.num < b.num;
    }
    return a.money > b.money;
}
int main() {
    using namespace std;
    int n, m;
    while (cin >> n >> m) {
        multiset<int> desk;
        vector<guest> people(m);
        long long ans = 0;
        for (int i = 0; i < n; i++) {
            int temp;
            cin >> temp;
            desk.insert(temp);
        }
        for (int i = 0; i < m; i++) {
            int a, b;
            cin >> a >> b;
            people[i].num = a;
            people[i].money = b;
        }
        sort(people.begin(), people.end(), cmp);
        for (int i = 0; i < m; i++) {
            if (desk.empty()) {
                break;
            }
            if (people[i].num <= *desk.rbegin()) {
                ans += people[i].money;
                desk.erase(desk.lower_bound(people[i].num));
            }
        }
        cout << ans << endl;
    }
    return 0;
}

//////////////////
自己动手/////
////////////////////
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
struct customer
{

    int number;
    int money;
};
bool compare(customer &a,customer&b)
{
    if(a.money==b.money)
        return a.number<b.number;
    return a.money>b.money;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        multiset<int>desk;
        int temp;
        for(int i=0;i<n;i++)
        {
            cin>>temp;
            desk.insert(temp);
        }
        vector<customer>people(m);
        for(int i=0;i<m;i++)
        {
            int a,b;
            cin>>a>>b;
            people[i].number=a;
            people[i].money=b;
        }
        sort(people.begin(),people.end(),compare);
        int ans=0;
        for(int i=0;i<m;i++)
        {
            if(desk.empty()) break;
            if(people[i].number<=*desk.rbegin())
            {
                ans+=people[i].money;
                //desk.erase(desk.lower_bound(people[i].num));
                desk.erase(desk.lower_bound(people[i].number));
            }
        }
        cout<<ans<<endl;
     }
    return 0;
}

1

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多