赞
踩
There are n meetings, and the start time and end time of i-th meeting are s i s_i si and e i e_i ei. Please design a greedy algorithm to find the minimum number of meeting rooms required to arrange all meetings.
给定一个会议时间安排的数组,每个会议时间都会包括开始和结束的时间 [[s1,e1],[s2,e2],…] (si < ei), 为避免会议冲突,同时要考虑充分利用会议室资源,请你计算至少需要多少间会议室,才能满足这些会议安排。
Input: [ [0, 20], [15, 20], [20, 23], [25, 30], [15, 25] ]
Output: 3
#include <bits/stdc++.h> //#define debug #define PII pair<int,int> #define x first #define y second using namespace std; const int N = 100; PII a[N]; vector<int> end_time_list;//现有会议室的结束时间集合 int main() { #ifdef debug freopen("in.txt", "r", stdin); #endif ios::sync_with_stdio(0); int n = 0; while (true) { int t1, t2; cin >> t1 >> t2; if (!t1 && !t2)break; a[n++] = {t1, t2}; } sort(a, a + n, [&](PII p1, PII p2) { return p1.x < p2.x; }); for (int i = 0; i < n; ++i) { int idx = 0;//最早结束会议室的下标 for (int j = 0; j < end_time_list.size(); ++j) { //找最早结束的会议 if (end_time_list[j] < end_time_list[idx]) idx = j; } if (end_time_list.size() && end_time_list[idx] <= a[i].x)//当前会议的开始时间在最早结束的后面,可以复用会议室 end_time_list[idx] = a[i].y; else //不能复用,新增一个会议室 end_time_list.push_back(a[i].y); } cout << end_time_list.size() << endl; return 0; }
#include <bits/stdc++.h> //#define debug #define PII pair<int,int> #define x first #define y second using namespace std; const int N = 100; PII a[N]; priority_queue<int, vector<int>, greater<int>> heap;//最早的结束时间 int main() { #ifdef debug freopen("in.txt", "r", stdin); #endif ios::sync_with_stdio(0); int n = 0; while (true) { int t1, t2; cin >> t1 >> t2; if (!t1 && !t2)break; a[n++] = {t1, t2}; } sort(a, a + n, [&](PII p1, PII p2) { return p1.x < p2.x; }); for (int i = 0; i < n; ++i) { if (!heap.empty() && heap.top() <= a[i].x) { //复用会议室 heap.pop(); } heap.push(a[i].y); } cout << heap.size() << endl; return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。