码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2376

时间:2016-05-12 22:25:21      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

Cleaning Shifts

Time Limit:1000MS    Memory Limit:65536KB  

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条线段,找出最少的数量,使得线段覆盖1-m。



做法:贪心,先排序,先开始的排前面,如果同时开始,那么晚结束的排前面,然后扫一遍即可。


#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct node{
	int x,y;
}e[25005];
bool cmp(node a,node b){
	if(a.x==b.x)return a.y>b.y;
	return a.x<b.x;
}
int main(){
	int n,m,i,j;
	cin>>n>>m;
	for(i=1;i<=n;i++){
		scanf("%d%d",&e[i].x,&e[i].y);
	}
	sort(e+1,e+1+n,cmp);//排序
	e[n+1].x=9999999;
	int ans=0,t=0,temp=0,f=0;//ans代表数量,t代表当前线段的最右端,temp代表t-i中右端最远的线段标号,f代表当前有没有线段可用
	for(i=1;i<=n;i++){
		if(t+1>=e[i].x){//如果当前线段的右端+1到达了当前线段的左端右边,那么
			if(temp<e[i].y){//寻找最右端的线段
				temp=e[i].y;
				f=1;
			}
			if(e[i+1].x>t+1&&f){//如果下一条线段的开始点已经大于当前线段的结束点+1,那么就把前面右端最远的线段用掉
				ans++;
				f=0;
				t=temp;
			}
		}
	}
	if(t<m)cout<<"-1"<<endl;
	else cout<<ans<<endl;
	return 0;
}



POJ 2376

标签:

原文地址:http://blog.csdn.net/black_miracle/article/details/51354371

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!