博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2376 Cleaning Shifts
阅读量:4668 次
发布时间:2019-06-09

本文共 2187 字,大约阅读时间需要 7 分钟。

Cleaning Shifts

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 9   Accepted Submission(s) : 2
Problem 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 <br> <br>* 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
 
每头牛有一定的工作时间段,问最少用多少头牛才能保证在所有时间内都有牛在工作
用贪心算法,每次选取工作结束时间最晚的牛。
记住:
2 10
1 5
1 6
也是可以的,不一定要1 5和5 10
 
#include 
#include
#include
#include
#include
using namespace std;struct node{ int s, e;}a[1000005];bool cmp(node a, node b)//贪心{ if (a.s == b.s)//开始时间短的在前 return a.e
> n >> t; for (i = 1; i <= n; i++) { cin >> a[i].s >> a[i].e; } a[n + 1].s = 999999999;//最后一个开始时间设为无穷大 sort(a + 1, a + 1 + n, cmp); int mm = 0; int ans = 0; bool f = 0; int ss =0; for (i = 1; i <= n; i++) { if (a[i].s <=ss+1 )//是ss+1 { if (a[i].e > mm) { mm = a[i].e; f = 1; } if (a[i + 1].s > ss+1 && f == 1) { ss = mm; ans++; f = 0; } } } if (ss>=t) cout << ans << endl; else cout << "-1" << endl; return 0;}

 

转载于:https://www.cnblogs.com/caiyishuai/p/8404859.html

你可能感兴趣的文章
UOJ 7 NOI2014 购票
查看>>
HDU1828 Picture 线段树+扫描线模板题
查看>>
利用jQuery点击DIV变颜色的小例子
查看>>
OpenStack Cinder发展动态系列--Austin峰会
查看>>
MySQL -- 行转列 -- GROUP_CONCAT -- MAX(CASE WHEN THEN)
查看>>
CSS魔法堂:hasLayout原来是这样!
查看>>
JS获取JSON对象数组某个属性最大值
查看>>
教程链接
查看>>
Spring MVC 文件上传 & 文件下载
查看>>
C++中print和printf的区别
查看>>
service程序改为windows窗体展示
查看>>
查询集 QuerySet
查看>>
ios 键盘的一些问题
查看>>
mac上使用终端生成RSA公钥和密钥
查看>>
jQuery-点击按钮页面滚动到顶部,底部,指定位置
查看>>
[原创]group by和compute 的使用
查看>>
9.13列表的用法
查看>>
secureCRT 如何上传下载文件
查看>>
Spring Cloud Config
查看>>
phoneGap实现离线缓存
查看>>