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

poj 2376 Cleaning Shifts

时间:2014-07-06 14:50:25      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   数据   os   

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时间 如果不能全覆盖就输出-1.

思路:贪心;

具体:先结构体排序,再递归找比当前数据的右端点小的左端点,如果有就标记继续递归最大的右端点,否则就return -1退出递归。

细节:第一位必须是1,在输入的时候就判断。

ac代码(效率不是太高):

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 int vis[1100009];
 6 int a,b;
 7 struct node
 8 {
 9     int a;
10     int b;
11 } d[25500];
12 int dig(int x,int y,int z)
13 {
14     int p=0,q=0,n=-10000;
15     if(y>=b)
16         return z;
17     for(int i=x; i<=a; i++)
18     {
19         if(!vis[i])
20             if(d[i].a<=y+1)
21             {
22                 vis[i]=1;
23                 p=1;
24                 if(d[i].b>=y+1)
25                     if(d[i].b>n)
26                     {
27                         q=i;
28                         n=d[i].b;
29                     }
30             }
31     }
32     if(p==0)
33         return -1;
34     dig(q,n,z+1);
35 }
36 int cmp(struct node a,struct node b)
37 {
38     if(a.a==b.a)
39         return a.b<b.b;
40     return a.a<b.a;
41 }
42 int main()
43 {
44     int i,j;
45     while(scanf("%d%d",&a,&b)!=EOF)
46     {
47         int flag=0;
48         memset(vis,0,sizeof(vis));
49         for(i=1; i<=a; i++)
50         {
51             int t;
52             scanf("%d%d",&d[i].a,&d[i].b);
53             if(d[i].a==1)
54             flag=1;
55         }
56         sort(d+1,d+a+1,cmp);
57         if(flag==0)
58         printf("-1\n");
59         else
60         printf("%d\n",dig(1,d[1].b,1));
61     }
62 }

 

 

poj 2376 Cleaning Shifts,布布扣,bubuko.com

poj 2376 Cleaning Shifts

标签:des   style   blog   color   数据   os   

原文地址:http://www.cnblogs.com/Xacm/p/3825571.html

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