標籤:
原題網址:https://open.kattis.com/problems/classrooms
Classrooms
The new semester is about to begin, and finding classrooms for orientation activities is always a headache.
There are k classrooms on campus and n proposed activities that need to be assigned a venue. Every proposed activity has specfic starting time si and ending time fi. Any such an activity should take place at one of the classrooms. Any of the k classrooms is big enough to hold any of the proposed activities, and each classroom can hold at most one activity at any time. No two proposed activities can take place at the same classroom at the same time. Even if two proposed activities overlap momentarily (the ending time of one activity equals the starting time another activity), they cannot be assigned to the same classroom.
There are so many proposed activities that there may not be enough classrooms to hold all the activities. It is desirable to have as many activities as possible. At most how many proposed activities can be assigned to the classrooms?
Input
- The first line contains two positive integers n and k (1≤k≤n≤200000 ), representing the number of proposed activities and number of classrooms, respectively.
- The following n lines each contains two positive integers: the ith line among these n lines contains si and fi (1≤si≤fi≤109), indicating the starting time and ending time of proposed activity i
Output
Output an integer indicating the maximum number proposed activities that can be scheduled.
Sample Input 1 |
Sample Output 1 |
4 2 1 4 2 9 4 7 5 8 |
3 |
Author(s): Lau Lap Chi
Source: Hong Kong Regional Online Preliminary 2016
題意,n個活動,k個教室,給定每個活動開始和結束時間,在同一個教室舉行的連續兩個活動結束時間和開始時間之間必須有間隔。問最多能舉辦多少個活動。
貪心,把每個活動按結束時間排序,然後從頭到尾掃一遍。
multiset裡存放每個教室進行中的活動的結束時間。
如果multiset裡有某個教室的活動在活動i開始之前就結束的,活動i就可以舉辦,把原來的結束時間刪掉,再把活動i的結束時間存進去。
如果multiset裡沒有比a[i].begin小的結束時間,即當前有活動的教室在活動i開始之前都結束不了活動,此時multiset裡元素的數量表示有多少個教室在同時進行活動,如果還有空教室,活動i就可以在這個教室進行,把活動i的結束時間存入multiset。
註:實際存入multiset的是 (-a[i].ed-1),而尋找時用的是(-a[i].begin)。因為要使用lower_bound函數,而lower_bound(start,end,k)返回的是集合裡大於等於k的第一個數的下標,而題目裡面要尋找的是 比 開始時間 小的 第一個 結束時間,加個負號就剛好。
#include <algorithm>#include <cstring>#include <string.h>#include <iostream>#include <list>#include <map>#include <set>#include <stack>#include <string>#include <utility>#include <vector>#include <cstdio>#include <cmath>#define LL long long#define N 200005#define INF 0x3ffffffusing namespace std;int n , m;struct node{ int bg,ed; //m每場活動開始時間,結束時間}a[N];bool cmp(node a , node b){ //按照結束時間排序 if(a.ed== b.ed) return a.bg < b.bg; return a.ed< b.ed;}int main(){ while (~scanf("%d%d" , &n , &m)) { for (int i = 0 ; i < n ; ++i) scanf("%d%d",&a[i].bg ,&a[i].ed); sort(a,a+n,cmp); multiset<int>endtime; //h存放每個教室進行中的活動的結束時間 endtime.clear(); int ans = 0; for (int i = 0 ; i < n ; ++i){ multiset<int> :: iterator iter; iter = endtime.lower_bound(-a[i].bg); //是否存在某個教室的活動在i開始時間前前就結束了 if (iter == endtime.end()){ //如果沒有在活動i開始前就結束活動的教室,就另找一個教室 if (endtime.size() < m){ endtime.insert(-a[i].ed- 1); ++ans; } continue; } endtime.erase(iter); //找到了某個教室活動已經結束了,活動i在這個教室進行 endtime.insert( - a[i].ed - 1); //更新活動的結束時間 ++ans; } printf("%d\n" , ans); }}
2016 acm香港網路賽 C題. Classrooms(貪心)