Cleaning Shifts
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice OpenJ_Bailian 2376
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.
题意:
FJ有N头牛,希望安排它们在时刻1到时刻T进行工作。每头牛都只能在它自己的一个特定工作的时间段(从时刻i到时刻j)进行工作。FJ想知道至少安排多少牛进行工作才能保证时刻1到T的任意时刻都会有至少一头牛在工作。
输入:
第一行N和T,之后N行给出每头牛的工作时间段。
输出:
至少安排的牛的头数。如果不存在方案则输出-1。
分析:
使用贪心策略,对每头牛按其时间段进行排序。由于需要覆盖整个时间区间,所以先按牛时间段的起始时间进刻由小到大进行排序。又由于需要minimize牛的数量,所有在之前排序的基础上再对牛时间段的结束时刻由大到小进行排序。然后遍历一遍排序好的牛区间就可以了。maxi变量来保存最大的右边界,tmp变量保存更新之后的当前右边界。对于当前的右边界tmp,不断向后遍历左边界不大于tmp+1的牛区间,用这些牛区间右边界最大值去更新maxi的值,如果这个值小于T,那么将tmp置为maxi,持续进行这个过程。
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <vector> 5 #include <string> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 const int MAX_N = 25000; 10 int N,T; 11 struct node{int st,ed;}n[MAX_N + 10]; 12 bool cmp(const node& n1,const node& n2){ 13 if(n1.st == n2.st) return n1.ed > n2.ed; 14 else return n1.st < n2.st; 15 } 16 int main(){ 17 int ans = 1; 18 scanf("%d%d",&N,&T); 19 for(int i = 0 ; i < N ; i++) 20 scanf("%d%d",&n[i].st,&n[i].ed); 21 sort(n,n + N,cmp); 22 if(n[0].st > 1) { 23 printf("-1 "); 24 return 0; 25 } 26 if(n[0].ed == T){ 27 printf("1 "); 28 return 0; 29 } 30 int tmp = n[0].ed; 31 int maxi = tmp; 32 for(int i = 1 ; i < N ; i++){ 33 if(n[i].st > tmp + 1){ 34 ans++; 35 tmp = maxi; 36 } 37 if(n[i].st <= tmp + 1){ 38 maxi = max(maxi,n[i].ed); 39 if(n[i].ed == T){ 40 ans++; 41 tmp = T; 42 break; 43 } 44 } 45 } 46 if(tmp == T){ 47 printf("%d ",ans); 48 } 49 else printf("-1 "); 50 return 0; 51 }