五月个人赛-a problem of DP(贪心,multiset)

来源:互联网 发布:python命令行运行文件 编辑:程序博客网 时间:2024/06/06 03:48

Problem Description

The final exam is coming, and finding classrooms for exams is always a headache for teachers , so teachers find you to assign the classrooms for exam.

There are n exams that need to be assigned and k classrooms that you can choose. Every proposed exam has starting time si and ending time ei. Any such an exam should take place at one of the classrooms. Any of the k classrooms is big enough to hold any number of the students that take the exam, and each classroom can hold at most one exam at any time, which means no two proposed exams can take place at the same classroom at the same time. Even if two proposed exams overlap momentarily (the ending time of one activity equals the starting time another activity), they can not be assigned to the same classroom.

There are so many proposed exam that there may not be enough classrooms to assign all the exams. It is desirable to have as many exams as possible. At most how many proposed exams can be assigned to the classrooms?

Input

There are T groups of test data. The first line contains one positive integer T(1<=T<=100).

For each test data : the first line contains two positive integers n and k (1<=k<=n<=200000 ), representing the number of proposed exams and number of classrooms, respectively.

The following n lines each contains two positive integers: the th line among these n lines contains si and ei (1<=si<=ei<=10^9), indicating the starting time and ending time of proposed exam i

Output

Output an integer indicating the maximum number proposed exams that can be scheduled.

Sample Input
24 23 72 68 94 510 11 22 33 44 55 66 77 88 99 1010 11
Sample Output
35

题意:
一共有T组数据。有k间空教室,n场考试需要安排,告诉了每场考试的开始时间和结束时间,求最多能安排多少场考试。

思路:
贪心的思路~
      如果两场考试都可以安排但不能同时安排,肯定选择结束时间早的那个;
      如果两场考试同时结束,选哪个都一样啦……

所以呀~ 每场考试按照结束的时间从早到晚进行排序,用multiset存每个教室结束考试的时间,如果任意一个教室都安排不下这个考试,即这个考试的起始时间在每个教室结束时间之前,那么这个考试可以直接舍弃,考虑下一个考试

注意:
1、一开始用的set, 将k个教室以0为终点同时放进去。。但是,set不会储存重复的数(微笑脸)。于是想能不能起始的时候以-i标志初始教室的结束时间,结果忘了安排考试的时候也会遇到同时结束的情况…… = =。 
2、学长推荐了两种解决方式, 
    a.用multiset           
    b.存教室的时候set存pair型,区分不同的教室
   觉得第一种方式比较简单一点,STL还是要多熟悉一下~

代码:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>#include <set>using namespace std;const int num = 200100;struct exam{    int s, e;    bool operator < (const exam& a) const    {        return e < a.e;    }};exam ex[num];multiset<long long> q;int main(){    //freopen("1.txt", "r", stdin);    int t;    cin >> t;    while( t --)    {        q.clear();        int n, k;        cin >> n >> k;        for(int i = 1; i <= n;  i++) cin >>ex[i].s >> ex[i].e;        for(int i = 1; i <= k; i ++) q.insert(0);        sort(ex + 1, ex + n + 1);        int cnt = 0;        for(int i = 1; i <= n; i ++)            {               multiset<long long>::iterator  it = q.lower_bound(ex[i].s);                 if(it != q.begin())                    {                        it --;                        q.erase(it);                        q.insert(ex[i].e);                        cnt ++;                    }            }    //    for(multiset<long long>::iterator  it = q.begin(); it != q.end(); it ++)   cout << *it << endl;        cout << cnt << endl;    }    return 0;}




原创粉丝点击