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?
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 i 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 an integer indicating the maximum number proposed exams that can be scheduled.
#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;
}