C语言排序(9)___选奶牛

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.


题目大意:
第一排输入两个整数N,T分别表示奶牛头数和工作时间段数.
接下来的N排分别是N头奶牛各自的工作时间开始和结束时间.
若同一时间段可以多个奶牛同时工作,但至少要一个奶牛.
输出奶牛最少需要多少头.如果存在一个时间段无法至少一个奶牛,那么输出-1


样例:

#include<stdio.h> 
#include<algorithm> 
using namespace std; 
struct COW        //定义存放奶牛工作信息的结构体
{ 
    int left; 
    int right; 
}cow[25001]; 
 
bool cmp(COW a,COW b)   //定义sort函数需要的比较函数,此函数为left的升序排列
{ 
    return a.left < b.left; 
} 
 
int main() 
{ 
      int i;
	int n,end,now,ans,book,now_right; //奶牛个数、工作结束时间、当前工作时间、已用奶牛个数、标记、能工作到最晚的时间
	while(scanf("%d%d",&n,&end)!=EOF)
	{
		for(i=0;i<n;i++)
			scanf("%d%d",&cow[i].left,&cow[i].right);
		sort(cow,cow+n,cmp);   //排序,以每头奶牛的左端点升序排列
		 if(cow[0].left >1)   //如果第一头牛都不能从开始工作,那么存在一个时间段无法至少一个奶牛.
        { 
            printf("-1\n"); 
            continue; 
        } 
		now=0;ans=0;i=0; //目前工作的时间点、当前奶牛次数
		while(now < end && i< n)  //当前工作点没有工作到结束并且奶牛没用完的时候进入.
		{
			book=0;now_right=0;    //标记,时间置为0
			while(cow[i].left<=now+1&&i<n) //当当前奶牛左端点能接上当前工作时间以及奶牛没用完的时候进入循环,旨在找到能工作到最晚的奶牛.
			{
				book=1;            //一旦有能接上当前时间的奶牛book赋值为1表示当前时间能继续有奶牛工作下去.
				if(cow[i].right>now_right)       //比较找到能工作到最晚的奶牛.
					now_right=cow[i].right;
				i++;    
			}
			if(book==0)    //book=0说明第二个while没有进.因为大循环进了满足i<n条件,那么cow[i].left<=now+1不满足,即是没有<span style="font-size: 10px; font-family: Arial, Helvetica, sans-serif;">能接上当前时间的奶牛</span>
				break;
			now=now_right;  //将当前工作时间变为已经找到的工作最晚的奶牛的工作结束时间.
			ans++;     //已用奶牛个数加1
		}
		if(!book || now < end) 判断是否book是否为0,为0则说明中间有段时间没奶牛可以工作,如果now<end说明奶牛工作最晚时间也不能达到工作结束时间.
            printf("-1\n"); 
        else
            printf("%d\n", ans);   
	}
    return 0; 
}  

本题心得:

这道题首先将所有奶牛以工作开始时间为准升序排列.那么左边的奶牛就是较右边的奶牛先工作的.从左边开始遍历每个奶牛.以能接上当前工作时间为准,不断找能够工作最晚的奶牛,然后将工作最晚的奶牛的工作结束时间赋值给当前时间,不断循环下去直到 1.无奶牛能接到当前时间——>book=0——>输出-1,    2.奶牛用完———>(1)当此时已经工作到结束——>now>=end——>输出ans.(2)没有工作到结束,那么已经无奶牛可用了——>输出-1.


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值