本题出自:    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 ;
}

Entertainment Box(可多执行的区间问题)的更多相关文章

  1. Entertainment Box Gym100781E(数据结构+贪心)

    Entertainment Box 题意: 有n个节目,每个节目给出开始时间(st)和结束时间(en): 有k个内存条这k个内存条可以同时存储节目.如果节目j的开始时间stj  大于等于节目i的结束时 ...

  2. 2015-2016 ACM-ICPC Nordic Collegiate Programming Contest ---E题Entertainment Box(有点变化的贪心)

    提交链接 http://codeforces.com/gym/100781/submit Description: Ada, Bertrand and Charles often argue over ...

  3. 计蒜客:Entertainment Box

    Ada, Bertrand and Charles often argue over which TV shows to watch, and to avoid some of their fight ...

  4. Nordic Collegiate Programming Contest 2015​ E. Entertainment Box

    Ada, Bertrand and Charles often argue over which TV shows to watch, and to avoid some of their fight ...

  5. MySQL 优化之EXPLAIN详解(执行计划)

    学习MySQL时我们都知道索引对于一个SQL的优化很重要,而EXPLAIN关键字在分析是否正确以及高效的增加了索引时起到关键性的作用. 这篇文章显示了如何调用“EXPLAIN”来获取关于查询执行计划的 ...

  6. 通过Vagrant搭建PHP环境(一) Vagrant box添加配置

    系统Windows10 Vagrant 1.8.1 VirtualBox 5.0.20 vagrant box下载地址:http://cloud.centos.org/centos/7/vagrant ...

  7. Vagrant 基础全面解析

    这篇 Vagrant 入门文章将带你创建一个 Vagrant 项目,这个过程将会用到 Vagrant 所提供的主要基本特性.如果想了解 Vagrant 能为你带来哪些好处,可以阅读 Vagrant 官 ...

  8. ASM文件系统

    1.确认数据库版本 2.个人理解的存储解决方案的发展趋势 2.1图示说明 2.2图示描述 如上图我们描述了在不同时期的IT行业(数据库)出现的存储文件系统,下面我们将分别说明: ü  裸设备:所谓裸设 ...

  9. JavaScript基础---语言基础(4)

    函数,对象和数组 学习要点: 1.函数声明 2.return返回值 3.arguments对象 4.Object类型 5.Array类型 6.对象中的方法 函数是定义一次但却可以调用或执行任意多次的一 ...

随机推荐

  1. 关于COM类工厂80070005和8000401a错误分析及解决办法

    关于COM类工厂80070005和8000401a错误分析及解决办法 看到很多相关的文章,第一次配置配置时没有啥作用,让别人来解决的,可惜不晓得他怎么解决的,当我再次遇到时,不得不硬着头皮去解决. 总 ...

  2. HDU 5649 DZY Loves Sorting(二分答案+线段树/线段树合并+线段树分割)

    题意 一个 \(1\) 到 \(n\) 的全排列,\(m\) 种操作,每次将一段区间 \([l,r]\) 按升序或降序排列,求 \(m\) 次操作后的第 \(k\) 位. \(1 \leq n \le ...

  3. 【译】第4节---简单的Code First示例

    原文地址:http://www.entityframeworktutorial.net/code-first/simple-code-first-example.aspx 假设我们要为XYZ学校创建一 ...

  4. Codeforces Beta Round #65 (Div. 2) C. Round Table Knights

    http://codeforces.com/problemset/problem/71/C 题意: 在一个圆桌上有n个人,每个人要么是1,要么就是0,现在要判断是否能由一些1相连构成正多边形. 思路: ...

  5. Codeforces Round #271 (Div. 2) F. Ant colony 线段树

    F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. 下载安装tomcat和jdk,配置运行环境,与Intellij idea 2017关联

    第一篇博客,最近公司要用java和jsp开发新的项目,第一次使用Intellij idea 2017,有很多地方需要一步步配置,有些按照网上的教程很快就配置好了,有的还是琢磨了一会儿,在这里做一个记录 ...

  7. String和常量池

    1.Java 会确保一个字符串常量只有一个拷贝 2.用new String() 创建的字符串不是常量,不能在编译期就确定,所以new String() 创建的字符串不放入常量池中,它们有自己的地址空间 ...

  8. linux学习笔记--程序与进程管理

    .工作管理 1.前台程序放后台程序  命令后 加  & 2.任务执行时将前台任务任务放到后台中并[暂停]  ctr + z 3.jobs 观察后台工作状态 及多少任务在执行,可以通过 help ...

  9. leecode第二十六题(删除排序数组中的重复项)

    class Solution { public: int removeDuplicates(vector<int>& nums) { int len=nums.size(); ) ...

  10. windows设置程序开机自启动

    在msconfig里面进行设置的前提是,这个程序已经在自启动列表中,只是没有被勾选上, 对于不在该启动列表里的程序需要: ①找到这个应用程序所在的位置; ②右击发送到桌面快捷方式;  ③在Window ...