2016 acm香港网络赛 C题. Classrooms(贪心)
原题网址:https://open.kattis.com/problems/classrooms
Classrooms
The new semester is about to begin, and finding classrooms for orientation activities is always a headache.
There are k classrooms on campus and n proposed activities that need to be assigned a venue. Every proposed activity has specfic starting time si and ending time fi. Any such an activity should take place at one of the classrooms. Any of the k classrooms is big enough to hold any of the proposed activities, and each classroom can hold at most one activity at any time. No two proposed activities can take place at the same classroom at the same time. Even if two proposed activities overlap momentarily (the ending time of one activity equals the starting time another activity), they cannot be assigned to the same classroom.
There are so many proposed activities that there may not be enough classrooms to hold all the activities. It is desirable to have as many activities as possible. At most how many proposed activities can be assigned to the classrooms?
Input
- The first line contains two positive integers n and k (1≤k≤n≤200000 ), representing the number of proposed activities and number of classrooms, respectively.
- The following n lines each contains two positive integers: the ith line among these n lines contains si and fi (1≤si≤fi≤109), indicating the starting time and ending time of proposed activity i
Output
Output an integer indicating the maximum number proposed activities that can be scheduled.
|
Sample Input 1 |
Sample Output 1 |
|
4 2 1 4 2 9 4 7 5 8 |
3 |
Author(s): Lau Lap Chi
Source: Hong Kong Regional Online Preliminary 2016
题意,n个活动,k个教室,给定每个活动开始和结束时间,在同一个教室举行的连续两个活动结束时间和开始时间之间必须有间隔。问最多能举办多少个活动。
贪心,把每个活动按结束时间排序,然后从头到尾扫一遍。
multiset里存放每个教室正在进行的活动的结束时间。
如果multiset里有某个教室的活动在活动i开始之前就结束的,活动i就可以举办,把原来的结束时间删掉,再把活动i的结束时间存进去。
如果multiset里没有比a[i].begin小的结束时间,即当前有活动的教室在活动i开始之前都结束不了活动,此时multiset里元素的数量表示有多少个教室在同时进行活动,如果还有空教室,活动i就可以在这个教室进行,把活动i的结束时间存入multiset。
注:实际存入multiset的是 (-a[i].ed-1),而查找时用的是(-a[i].begin)。因为要使用lower_bound函数,而lower_bound(start,end,k)返回的是集合里大于等于k的第一个数的下标,而题目里面要查找的是 比 开始时间 小的 第一个 结束时间,加个负号就刚好。
#include <algorithm>
#include <cstring>
#include <string.h>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <cstdio>
#include <cmath> #define LL long long
#define N 200005
#define INF 0x3ffffff using namespace std; int n , m;
struct node{
int bg,ed; //m每场活动开始时间,结束时间
}a[N]; bool cmp(node a , node b){ //按照结束时间排序
if(a.ed== b.ed) return a.bg < b.bg;
return a.ed< b.ed;
} int main(){
while (~scanf("%d%d" , &n , &m))
{
for (int i = ; i < n ; ++i)
scanf("%d%d",&a[i].bg ,&a[i].ed);
sort(a,a+n,cmp); multiset<int>endtime; //h存放每个教室正在进行的活动的结束时间
endtime.clear();
int ans = ;
for (int i = ; i < n ; ++i){
multiset<int> :: iterator iter;
iter = endtime.lower_bound(-a[i].bg); //是否存在某个教室的活动在i开始时间前前就结束了
if (iter == endtime.end()){ //如果没有在活动i开始前就结束活动的教室,就另找一个教室
if (endtime.size() < m){
endtime.insert(-a[i].ed- );
++ans;
}
continue;
}
endtime.erase(iter); //找到了某个教室活动已经结束了,活动i在这个教室进行
endtime.insert( - a[i].ed - ); //更新活动的结束时间
++ans;
}
printf("%d\n" , ans);
}
}
2016 acm香港网络赛 C题. Classrooms(贪心)的更多相关文章
- 2016 acm香港网络赛 B题. Boxes
原题网址:https://open.kattis.com/problems/boxes Boxes There are N boxes, indexed by a number from 1 to N ...
- 2016 acm香港网络赛 A题. A+B Problem (FFT)
原题地址:https://open.kattis.com/problems/aplusb FFT代码参考kuangbin的博客:http://www.cnblogs.com/kuangbin/arch ...
- 2016 acm香港网络赛 F题. Crazy Driver(水题)
原题网址:https://open.kattis.com/problems/driver Crazy Driver In the Linear City, there are N gates arra ...
- hihoCoder #1388 : Periodic Signal ( 2016 acm 北京网络赛 F题)
时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal processing. He has a device w ...
- hdu 5881 Tea (2016 acm 青岛网络赛)
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5881 Tea Time Limit: 3000/1000 MS (Java/Others) Me ...
- HDU 5901 Count primes (2016 acm 沈阳网络赛)
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5901 题意:输入n,输出n以内质数个数 模板题,模板我看不懂,只是存代码用. 官方题解链接:https ...
- 2013 acm 长沙网络赛 G题 素数+枚举 Goldbach
题目 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3856 先预处理求出两个素数的和与积,然后枚举n-prime和n/pr ...
- 2018 ACM南京网络赛H题Set解题报告
题目描述 给定\(n\)个数$a_i$,起初第\(i\)个数在第\(i\)个集合.有三种操作(共\(m\)次): 1 $u$ $v$ 将第$u$个数和第$v$个数所在集合合并 2 $u$ 将第$u$个 ...
- ACM-ICPC 2019南昌网络赛F题 Megumi With String
ACM-ICPC 南昌网络赛F题 Megumi With String 题目描述 给一个长度为\(l\)的字符串\(S\),和关于\(x\)的\(k\)次多项式\(G[x]\).当一个字符串\(str ...
随机推荐
- md5是哈希算法的改进加强,因为不同原始值可能hash结果一样,但md5则改善了用于验证消息完整性,不同md5值原始值也必将不一样
md5是哈希算法的改进加强,因为不同原始值可能hash结果一样,但md5则改善了用于验证消息完整性,不同md5值原始值也必将不一样
- 为什么代理属性设置成assign为了防止生成保留环来
循环引用 全部的引用计数系统, 都存在循环应用的问题, 比如以下的引用关系: 1. 对象a创建并引用到了对象b 2. 对象b创建并引用到了对象c 3. 对象c创建并引用到了对象b 这时候b和c的引用计 ...
- pc_lint的用法转
PC-Lint是一款C/C++软件代码静态分析工具,不仅可以检查一般的语法错误,还可以检查潜在的错误,比如数组访问越界.内存泄漏.使用未初始化变量.使用空指针等.在单元测试前使用PC-Lint来检查代 ...
- [转载]CentOS 6.5 安装五笔输入法
FROM:http://blog.sina.com.cn/s/blog_49d6d41c0101i0zs.html 1.一般安装了中文环境会默认安装了好多输入法,先删除了ibus sudo yum ...
- Linux学习之三-Linux系统的一些重要配置文件
Linux学习之三-Linux系统的一些重要配置文件 1.网卡配置文件 /etc/sysconfig/network-scripts/ifcfg-eth0 说明: DEVICE=eth0 ...
- 2017.6.29 java读取.properties配置文件的几种方法
参考来自:http://www.cnblogs.com/s3189454231s/p/5626557.html 关于路径的解释:http://blog.csdn.net/bluishglc/artic ...
- 2013年新统计全国省市县以及邮政编码SQL数据脚本
USE [imei8com] GO /****** Object: Table [dbo].[Zone] Script Date: 03/12/2014 15:05:41 ******/ SET AN ...
- iPhone手机解锁效果&&自定义滚动条&&拖拽--Clone&&窗口拖拽(改变大小/最小化/最大化/还原/关闭)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 排查 “Detected Tx Unit Hang”问题
实现功能: 使用自己已经分配的内存让skb->data指向,而不是使用alloc_malloc(). 部分代码如下: /* * build a new sk_buff */ //struct s ...
- mongo: 索引
索引创建 在学习索引之前,我们先看一下,如果没有添加索引时,我们用explain()函数,查看查询计划是什么样的. 发现使用的是BasicCursor,那么就代表我们没有索引,当我们查某一个数据的时候 ...