ACM学习历程——POJ 2376 Cleaning Shifts(贪心)
Description
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
* 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
Sample Input
3 10
1 7
3 6
6 10
Sample Output
2
Hint
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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <string>
#define inf 0x3fffffff
#define eps 1e-10 using namespace std; struct node
{
int Begin, End;
}a[25005]; bool cmp(node x, node y)
{
if (x.Begin != y.Begin)
return x.Begin < y.Begin;
else
return x.End > y.End;
} int t, n; int qt()
{
int ans = 0;
int now, p, k = 0, Max;
now = 1;
while (now <= t)
{
if (k == n)
return -1;
if (a[k].Begin > now)
return -1;
Max = a[k].End;
while (a[k].Begin <= now && k < n)
{
Max = max(Max, a[k].End);
k++;
}
now = Max + 1;
ans++;
}
return ans;
} int main()
{
//freopen("test.txt", "r", stdin);
while (scanf("%d%d", &n, &t) != EOF)
{
for (int i = 0; i < n; ++i)
scanf("%d%d", &a[i].Begin, &a[i].End);
sort(a, a+n, cmp);
printf("%d\n", qt());
}
return 0;
}
ACM学习历程——POJ 2376 Cleaning Shifts(贪心)的更多相关文章
- POJ 2376 Cleaning Shifts 贪心
Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...
- POJ - 2376 Cleaning Shifts 贪心(最小区间覆盖)
Cleaning Shifts Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some clea ...
- poj 2376 Cleaning Shifts 贪心 区间问题
<pre name="code" class="html"> Cleaning Shifts Time Limit: 1000MS Memory ...
- POJ 2376 Cleaning Shifts (贪心,区间覆盖)
题意:给定1-m的区间,然后给定n个小区间,用最少的小区间去覆盖1-m的区间,覆盖不了,输出-1. 析:一看就知道是贪心算法的区间覆盖,主要贪心策略是把左端点排序,如果左端点大于1无解,然后, 忽略小 ...
- POJ 2376 Cleaning Shifts(轮班打扫)
POJ 2376 Cleaning Shifts(轮班打扫) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Farmer ...
- poj 2376 Cleaning Shifts
http://poj.org/problem?id=2376 Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 2376 Cleaning Shifts【贪心】
POJ 2376 题意: 给出一给大区间和n各小区间,问最少可以用多少小区间覆盖整个大区间. 分析: 贪心法.设t为当前所有已确定区间的最右端,那我们可以每次都取所有可选的小区间(左端点<=t+ ...
- 【原创】poj ----- 2376 Cleaning Shifts 解题报告
题目地址: http://poj.org/problem?id=2376 题目内容: Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K ...
- poj 2376 Cleaning Shifts 最小区间覆盖
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40751 Accepted: 9871 ...
随机推荐
- jsp获取web.xml 里的配置项
ServletContext servletContext = request.getSession().getServletContext(); String titl ...
- 自己编译GCC(compile gcc from source)
有的时候,我不是第一次遇到这种时候,编译内核时报出编译器BUG.如果是ubuntu还好一点,默认软件仓库中就有好几个GCC,换一换总能找到一个好使的,实在不行还有个Tooltrain的ppa,但Deb ...
- java String概述
class StringDemo { public static void main(String[] args) { String s1 = "abc";//s1 是一个类类 ...
- Win7获取管理权限修改Host文件以其他权限问题
——win7管理员权限修改Host文件——百度经验 win7管理员权限获取办法: win7管理员权限 获取办法——经验——百度 win7最高权限获取: win7最高权限 获取——百度经验
- Using ADO.NET Data Service
ADO.NET Data Service是随同Visual Studio 2008 SP1提供的用于构建在数据对象模型 (如EF-DEMX, LINQ-DBML) 之时来快速提供企业网内外的轻量级数据 ...
- 公网yum 源地址
1. centos5.* 公网yum 源地址 [root@web ~]# cd /etc/yum.repos.d/[root@web yum.repos.d]# wget -O /etc/yum.r ...
- npm install --save 、--save-dev 、-D、-S 的区别与NODE_ENV的配置
https://blog.csdn.net/jwl_willon/article/details/81054978 1.npm install <=> npm i --save < ...
- 【BZOJ3997】[TJOI2015]组合数学 最长反链
[BZOJ3997][TJOI2015]组合数学 Description 给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走.问至少走多少次才能将财宝捡完.此对此问题变形,假设每个格 ...
- 九度OJ 1146:Flipping Pancake(翻饼子) (递归、游戏)
时间限制:1 秒 内存限制:32 兆 特殊判题:是 提交:265 解决:116 题目描述: We start with a stack n of pancakes of distinct sizes. ...
- sed相关
1 global flag sed 's/xxx/xxx/' inputfile,如果没有带global flag g的话,匹配替换的只是inputfile中的每一行的第一个匹配项.如果带了g的话,才 ...