POJ 2376- Cleaning Shifts(贪心)

G - Cleaning Shifts
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  POJ 2376
Appoint description:  System Crawler  (2016-04-29)

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
 
    
题意:
有一些奶牛,每只奶牛负责一个时间段。问覆盖完全部的时间段最少需要多少只奶牛。若不能全部覆盖,输出-1.
 
    
AC代码:
 
    
/*
这个题目看懂了一点,原来1-5,6-10也是算一个连续的
区间段的,一开始我觉得应该不算,所以Wa了一次。
*/


#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<queue>
#include<vector>
#include<set>
using namespace std;
const int T=35000;
#define inf 0x3f3f3f3fL
typedef long long ll;

struct node
{
	int L,R;
}a[T];

bool cmp(const node& a,const node& b){
	return a.L<b.L||(a.L==b.L&&a.R>b.R);
}

int main()
{
#ifdef zsc
	freopen("input.txt","r",stdin);
#endif

	int n,m,i,j,k;
	while(~scanf("%d%d",&n,&m))
	{
		for(i=0;i<n;++i){
			scanf("%d%d",&a[i].L,&a[i].R);
		}
		sort(a,a+n,cmp);
		int cnt = 1,mar = a[0].R;
		bool flag = false;
		if(a[0].L!=1){
			printf("-1\n");
			continue;
		}
		for(i=1;i<n;++i){
			j = i;
			int tmp = 0;
			if(a[j].L-1>mar){
				flag=true;
				break;
			}
			while(a[j].L-1<=mar&&j<n)
			{
				if(a[j].R>mar)
				tmp = max(tmp,a[j].R);
				j++;
			}
			if(tmp!=0)cnt++,mar=tmp;
			i = j-1;
		}
		if(flag||mar<m){//判断是否最大值超过区间末端,和中间没断开
			printf("-1\n");
		}
		else{
			printf("%d\n",cnt);
		}
	}
	return 0;
}


你可能感兴趣的:(贪心)