计蒜客:Entertainment Box

左心房为你撑大大i 提交于 2020-10-30 03:41:36

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, k(1 \le k < n \le 100 000)(1k<n100000). 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 0000xi<yi1000000000.

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):

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<vector>
 4 using namespace std;
 5 
 6 int n, k, c, ans=0;
 7 
 8 struct node{
 9     int start;
10     int end;
11     int vis;
12 }a[100001]; 
13 
14 vector<int> q;
15 
16 bool cmp(node x, node y){
17     return x.end < y.end;
18 }
19 
20 int main(){
21     cin >> n >> k;
22     for(int i=0;i<n;i++){
23         cin >> a[i].start >> a[i].end;
24     } 
25     sort(a, a+n, cmp);
26     for(int i=0;i<k;i++)q.push_back(0);
27     for(int i=0;i<n;i++){
28             vector<int>::iterator count = upper_bound(q.begin(), q.end(), a[i].start);
29             if(count != q.begin()){
30                 q.erase(count-1);
31                 q.push_back(a[i].end); 
32                 ans++;
33             }
34     }
35     cout << ans;
36 }

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!