无题 (贪心)

xiaoxiao2021-02-28  107

无题

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; } } }
转载请注明原文地址: https://www.6miu.com/read-97078.html

最新回复(0)