基准时间限制:1 秒 空间限制:131072 KB 分值: 10
难度:2级算法题
有若干个活动,第i个开始时间和结束时间是[Si,fi),同一个教室安排的活动之间不能交叠,求要安排所有活动,最少需要几个教室?
Input
第一行一个正整数n (n <= 10000)代表活动的个数。 第二行到第(n + 1)行包含n个开始时间和结束时间。 开始时间严格小于结束时间,并且时间都是非负整数,小于1000000000
Output
一行包含一个整数表示最少教室的个数。
Input示例
3 1 2 3 4 2 9
Output示例
2
AC:代码
#include <bits/stdc++.h> #define LL long long using namespace std; const int AX = 1e4+666; struct Node { LL l , r ; }e[AX]; bool cmp( const Node &a , const Node &b ){ if( a.l == b.l ) return a.r < b.r; else return a.l < b.l; } bool operator<( Node a , Node b ){ if( a.r == b.r ) return a.l < b.l; else return a.r > b.r; } priority_queue<Node>que; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n ; for( int i = 0 ; i < n ; i++ ){ cin >> e[i].l >> e[i].r ; } sort(e,e+n,cmp); int res = 1; que.push(e[0]); for( int i = 1 ; i < n ; i++ ){ if( que.top().r <= e[i].l ){ que.pop(); que.push(e[i]); }else{ que.push(e[i]); res++; } } cout << res << endl; return 0; }
ios_base::sync_with_stdio(false); cin.tie(0); //看不懂请参见点击打开链接