poj 2376 && bzoj 3389: [Usaco2004 Dec]Cleaning Shifts安排值班(贪心)

3389: [Usaco2004 Dec]Cleaning Shifts安排值班

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 462   Solved: 175
[ Submit][ Status][ Discuss]

Description

    一天有T(1≤T≤10^6)个时段.约翰正打算安排他的N(1≤N≤25000)只奶牛来值班,打扫
打扫牛棚卫生.每只奶牛都有自己的空闲时间段[Si,Ei](1≤Si≤Ei≤T),只能把空闲的奶牛安排出来值班.而且,每个时间段必需有奶牛在值班.  那么,最少需要动用多少奶牛参与值班呢?如果没有办法安排出合理的方案,就输出-1.

Input

    第1行:N,T.
    第2到N+1行:Si,Ei.

Output

    最少安排的奶牛数.

Sample Input

3 10
1 7
3 6
6 10

Sample Output

2


所有时间段按x从小到大排序,x相同按y从大到小排

之后贪心,必选第一个,假设第一个的覆盖区间为[1, y],那么后面所有起点在[1, y+1]内的线段找个y最大的选上,以此类推到最后一个时间段,中间如果有断或者第一个区间不从1开始就是-1


#include<stdio.h>
#include<algorithm>
using namespace std;
typedef struct Line
{
	int x, y;
	bool operator < (const Line &b) const
	{
		if(x<b.x || x==b.x && y>b.y)
			return 1;
		return 0;
	}
}Line;
Line s[25005];
int main(void)
{
	int n, len, i, ans, now, bet;
	scanf("%d%d", &n, &len);
	len++;
	for(i=1;i<=n;i++)
	{
		scanf("%d%d", &s[i].x, &s[i].y);
		s[i].y++;
	}
	sort(s+1, s+n+1);
	if(s[1].x!=1)
	{
		printf("-1\n");
		return 0;
	}
	bet = now = s[1].y, ans = 1;
	for(i=1;i<=n;i++)
	{
		if(s[i].x<=now)
			bet = max(bet, s[i].y);
		else
		{
			if(bet<s[i].x)
			{
				printf("-1\n");
				return 0;
			}
			ans++;
			now = bet;
			bet = max(bet, s[i].y);
		}
	}
	if(now<len)
	{
		if(bet<len)
			printf("-1\n");
		else
			printf("%d\n", ans+1);
	}
	else
		printf("%d\n", ans);
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值