一、题目

Description

Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T. 

Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval. 

Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.

Input

* Line 1: Two space-separated integers: N and T 

* Lines 2..N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.

Output

* Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.

Sample Input

3 10
1 7
3 6
6 10

Sample Output

2

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed. 

INPUT DETAILS: 

There are 3 cows and 10 shifts. Cow #1 can work shifts 1..7, cow #2 can work shifts 3..6, and cow #3 can work shifts 6..10. 

OUTPUT DETAILS: 

By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.

二、思路&心得

  • 利用贪心算法,先按开始时间将所有数据进行排序,然后每次选择结束时间最大的即可

三、代码

#include<cstdio>
#include<algorithm>
#define MAX_N 25005
using namespace std; typedef pair<int, int> P; int N, T; int cnt; P a[MAX_N]; bool cmp(P a, P b) {
if (a.first < b.first) return true;
else return false;
} int solve() {
sort(a, a + N, cmp);
bool found = false;
int begin = 0, end = 0;
for (int i = 0; i < N; i ++) {
if (a[i].first <= begin + 1) {
if (!found) found = true;
if (a[i].second > end) {
end = a[i].second;
if (end == T) return ++ cnt;
}
continue;
}
if (!found ) return -1;
begin = end;
found = false;
cnt ++;
i --;
}
return -1;
} int main() {
scanf("%d %d", &N, &T);
cnt = 0;
for (int i = 0; i < N; i ++) {
scanf("%d %d", &a[i].first, &a[i].second);
}
printf("%d\n", solve());
return 0;
}

最新文章

  1. 2013 duilib入门简明教程 -- 响应按钮事件(4)
  2. windows平台下基于VisualStudio的Clang安装和配置
  3. web项目 验证码 *** 最爱那水货
  4. SQL速记
  5. B树算法与实现 (C语言实现)
  6. 注解:【有连接表的】Hibernate单向N-&gt;N关联
  7. iOS开发之应用内检测手机锁屏,解锁状态
  8. shell编程报错 [: missing `]&#39;
  9. BestCoder Round #60 题解链接
  10. codeigniter中base_url和site_url
  11. Android实现简单短信发送器
  12. SAP连接HANA数据库
  13. 关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究
  14. 幻灯片slider
  15. andori 动画验证必填项
  16. 一张图,理解JAVA体系结构、运行机制、JVN运行机制、Java平台(初学)
  17. 如何刻录cd音乐
  18. Ubuntu 18.04 修改gedit的配色方案
  19. Hadoop日记Day13---使用hadoop自定义类型处理手机上网日志
  20. Jquery中bind(), live(), on(), delegate()四种注册事件的优缺点,建议使用on()

热门文章

  1. 在CentOS7.6上安装自动化运维工具Ansible以及playbook案例实操
  2. linux查看日志文件内容命令(面试被常问到的问题)
  3. redis缓存数据库入门教程
  4. WPF实现斜纹圆角进度条样式
  5. sqlplus 格式化一例
  6. 团队合作开发git冲突解决方案 Intellij IDEA
  7. tcp三次握手四次挥手那些事
  8. 硬盘空间术语:unallocated, unused and reserved
  9. window.location.hash 页面跳转,精确定位,实例展示:
  10. Restful和WeBAPI学习笔记