无题 (贪心)

无题

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 82 Accepted Submission(s): 60
 
Problem Description
就要复试了,外地的考生都要在学校附近住宾馆了。假设在学校附近有C家宾馆,并且这些宾馆只有单人房,而每家宾馆的价格不一样,学生们都想找价格便宜的住,所以现在需要你的帮助,当有学生需要住宾馆的时候,告诉他哪个宾馆还有空的房间并且价格最便宜。而且有一个要求,同一个组的学生要住在同一个宾馆。
 
Input
输入包括多组数据。输入首先包括一个整数T(T <= 50),代表有T组数据。
每组数据首先是一个整数C(C <= 100),代表宾馆的个数,接下来是C行数据,每行3个整数,第一个代表宾馆的编号(<=1000),第二个是宾馆的房间数(<=50),第三个是宾馆的价格(<=1000)。
然后是一个整数T (T <= 1000),代表想找宾馆住的小组,接下来的T行每行代表一个要找宾馆的小组,每个小组不超过10人。
 
Output
对于每组数据中的想找宾馆的小组,输出他们应该找的宾馆编号。如果没有合适的宾馆或已经住满,输出”sorry”.
 
Sample Input
1
2
1 2 100
2 3 120
4
3 
1
1
5
 
Sample Output
2
1
1
sorry
 
Author
8600
 
Source
2008浙大研究生复试热身赛(2)——全真模拟
 

贪心,先把价格从小到大排个序,然后挨个挨个来,找不到合适的就输出sorry
#include <iostream>
#include <queue>
#include <algorithm>
#include <string.h>
using namespace std;
struct Hotel {
    int num;
    int people;
    int much;
}hotel[105];
int main() {
    int T;
    cin >> T;
    while (T--) {
        memset(hotel, 0, sizeof(hotel));
        int c;
        cin >> c;
        for (int i = 0; i < c; ++i) {
            cin >> hotel[i].num >> hotel[i].people >> hotel[i].much;
        }
        sort(hotel, hotel + c, [](Hotel h1, Hotel h2)->bool{return h1.much < h2.much || (h1.much == h2.much && h1.people > h2.people);});
        int cc;
        cin >> cc;
        
        for (int i = 0; i < cc; ++i) {
            int flag = 0;
            int a;
            cin >> a;
            for (int j = 0; j < c; ++j) {
                if (a <= hotel[j].people) {
                    flag = 1;
                    hotel[j].people -= a;
                    cout << hotel[j].num << endl;
                    break;
                }
            }
            if (!flag)
                cout << "sorry" << endl;
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值