2016/10/8 1001. 舞蹈室安排

来源:互联网 发布:电脑网络连接在哪里找 编辑:程序博客网 时间:2024/05/11 05:53

又是一个新的套路,首先将每个时间段按结束时间排序后,遍历每个时间段,如果开始时间比前一个的结束时间晚就可以安排下去,这样子的通过数是最多的。

#include <iostream>#include <algorithm>using namespace std;class Time{public:Time(int a = 0, int b = 0){begin = a;end = b;}int begin;int end;}time[150];bool cmp(Time a, Time b){return a.end < b.end;}int main(){int n;cin >> n;while (n--){int m;cin >> m;int total = m;while (m){int a, b;cin >> a >> b;time[total - m] = Time(a, b);m--;}sort(time, time + total, cmp);int ans = 0;Time temp;for (int i = 0; i <= total - 1; i++){if (i == 0){ans++;temp = time[i];continue;}if (time[i].begin > temp.end){temp = time[i];ans++;}}cout << ans << endl;}}



0 0
原创粉丝点击