本题出自:    Nordic Collegiate Programming Contest 2015​

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 k different TV shows simultaneously, and whenever a show recorded in one the machine’s k 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 The first line of input contains two integers n,k (1 ≤ k < n ≤ 100 000). Then follow n lines, each containing two integers xi,yi, meaning that show i starts at time xi and finishes by time yi. This means that two shows i and j, where yi = xj, can be recorded, without conflict, in the same recording slot. You may assume that 0 ≤ xi < yi ≤ 1 000 000 000. Output 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.

Sample 1:

3 1

1 2

2 3

2 3

2

Sample 2:

4 1

1 3

4 6

7 8

2 5

3

Sample 3:

5 2

1 4

5 9

2 7

3 8

6 10

3

题意:输入n,k 表示有n场电视节目,最多可以同时录制k场,然后n行输入电视节目的起始时间和结束时间,注意(x1,y1) (x2,y2) y1==x2 不算重叠,求能录制的最多数量;

思路:定义多重集合 multiset<int>q;  插入k个0,表示同时录制时已经录制完部分电视节目的结束时间,把n个电视节目按结束时间排序,依次进行处理,如果当前电视节目的起始时间比集合中的k个数都要小,则表示当前节目不能放在k个节目任何一个节目后录制,则跳过;否则,在k个结束时间中找一个小于等于(最接近的,贪心原则)当前节目开始时间的值(这也是我们写比较函数的原因),然后删掉更新为当前节目的结束时间;

要点:这是第一次接触迭代器(iterator),暂时可以先将其当做指针来理解,upper_bound()和lower_bound都是STL库里很方便进行查找的函数,两者区别是前者不带等号而后者带(即在序列里若遇到相等情况前者返给迭代器相等值后一位而后者就返回相等值那一位)。然后sort函数里本身还是可以写比较函数的,且不编写函数时用sort排序pair数列,是按照.first的值从小到大排的,另外要注意set是不能存储相同的值的,故必须改成multiset.

代码如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
using namespace std;
typedef pair<int,int> p;//方便书写
bool cmp(const p&a ,const p&b){//修改sort规则,在.first(结束时间)相同情况下.second(开始时间)大的在前面
if(a.first == b.first)return a.second > b.second;
return a.first < b.first;
}
multiset <int> myset;//定义好multiset
multiset<int>::iterator ite;
int main(){
int n,k;
cin>>n>>k;
p a[n];
int ans=;
for(int i=;i<n;i++){
cin>>a[i].second;
cin>>a[i].first;
}
sort(a,a+n,cmp);
for(int i=;i<k;i++)
myset.insert();//根据k的值来决定set集合里的数目
for(int i=;i<n;i++){
ite=myset.upper_bound(a[i].second);//将开始时间与序列内的做比较,只要比最小的结束时间大就行
if(ite==myset.begin()) continue;//如果位于首位即开始时间小于最快的结束时间,无法加入,下一个
ite--;
myset.erase(ite);//若能成功插入则将符合条件的结束时间最小于接近的删除掉;
ans++;
myset.insert(a[i].first);
}
cout<<ans<<endl;
return ;
}

最新文章

  1. 深入理解Spring MVC
  2. C++中的 :: 用法
  3. Env:autojump安装使用
  4. linux系统的文件类型学习
  5. activemq 的小实验
  6. epub显示特殊字体
  7. Linq 调试
  8. L - Kakuro Extension - HDU 3338 - (最大流)
  9. react学习笔记2--练习Demos
  10. [内存管理]管理图解v0.1 v0.2 v0.3
  11. GIT工程迁移方法总结
  12. Sublime Text 3安装Package Control快速建立html5和xhtml文档
  13. 动手动脑(lesson 3)
  14. c# Bitmap byte[] Stream 文件相互转换
  15. 136 Ugly Numbers(priority_queue+逆向求解要求数)
  16. Java编程的逻辑 (1) - 数据和变量
  17. INTRO: THE DAWN (亡灵序曲) 中独白
  18. Spring Boot(5) 集成Hibernate 日志配置
  19. 解决二维数组转为ArrayList集合问题
  20. Fragment,仿QQ空间

热门文章

  1. phpcms9-6-0 一键getshell工具
  2. springmvc上传zip文件并解压缩代码示例
  3. js try catch 的使用,容错处理
  4. openlayers空间点查询之GetFeatureInfo
  5. 不要再混淆js的substring和substr了!(附js所有字符串方法)
  6. hdu 5212 Code 筛法或者莫比乌斯
  7. 2.0 vue内置指令与自定义指令
  8. php中if(\$a==\$b)和if(\$a=\$b)什么区别?
  9. 前端调用接口报错看不到报错响应时 console.dir
  10. numpy广播