Ada, Bertrand and Charles often argue over which TV shows to watch, and to avoid some of their fights they have finally decided to buy a video tape recorder. This fabulous, new device can record kk different TV shows simultaneously, and whenever a show recorded in one the machine's kk slots ends, the machine is immediately ready to record another show in the same slot.

The three friends wonder how many TV shows they can record during one day. They provide you with the TV guide for today's shows, and tell you the number of shows the machine can record simultaneously. How many shows can they record, using their recording machine? Count only shows that are recorded in their entirety.

Input Format

The first line of input contains two integers nn, kk (1 \le k < n \le 100 000)(1≤k<n≤100000). Then follow nn lines, each containing two integers x_i, y_ixi​,yi​, meaning that show ii starts at time x_ixi​ and finishes by time y_iyi​. This means that two shows iiand jj, where y_i = x_jyi​=xj​, can be recorded, without conflict, in the same recording slot. You may assume that 0 \le x_i < y_i \le 1 000 000 0000≤xi​<yi​≤1000000000.

Output Format

The output should contain exactly one line with a single integer: the maximum number of full shows from the TV guide that can be recorded with the tape recorder.

样例输入1

3 1
1 2
2 3
2 3

样例输出1

2

样例输入2

4 1
1 3
4 6
7 8
2 5

样例输出2

3

样例输入3

5 2
1 4
5 9
2 7
3 8
6 10

样例输出3

3

题目来源

Nordic Collegiate Programming Contest 2015​


  

  2018/7/26 计蒜客 Nordic Collegiate Programming Contest 2015​ 比赛的E题,这个题做了好一会,总结一下。

  这个题是一道类似贪心的题,我一开始考虑的是,按照结束时间从低到高进行排序,从第一个插槽开始走到底,走完再走下一条插槽的情况,但是一直WA,在队友提醒下改为类似并行的解决办法。

  

  先进行按照结束时间从低到高进行排序,然后开辟一个q数组,有几个插槽就开几位,用来存放当前最后结束的影片的时间,然后循环,判断当前的开始时间和q数组内的时间的大小,当该开始

时间比q数组的时间都小的时候,就不插入。当开始时间比数组内的某个值大的时候,就删除这个值,把当前影片的结束时间插入其中。

  代码如下(已AC):

 #include<iostream>
#include<algorithm>
#include<vector>
using namespace std; int n, k, c, ans=; struct node{
int start;
int end;
int vis;
}a[]; vector<int> q; bool cmp(node x, node y){
return x.end < y.end;
} int main(){
cin >> n >> k;
for(int i=;i<n;i++){
cin >> a[i].start >> a[i].end;
}
sort(a, a+n, cmp);
for(int i=;i<k;i++)q.push_back();
for(int i=;i<n;i++){
vector<int>::iterator count = upper_bound(q.begin(), q.end(), a[i].start);
if(count != q.begin()){
q.erase(count-);
q.push_back(a[i].end);
ans++;
}
}
cout << ans;
}
04-15 13:23