Cleaning Shifts POJ - 2376
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.
题目并不难,贪心解决,区间问题,可怕的是我就因为一个细节问题没考虑到wrong了十几次,深刻的教训,这个细节就是cow的shift是可以大于T的,在它已经大于T的时候要退出循环,加上条件break后就可以ac了
有兴趣的小伙伴可以看看代码,加油加油
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <stack>
using namespace std;
#define N 25003
#define PI 3.14159265358979323
#define INF 0x3f3f3f3f
#define MOD 100000007
#define ll long long
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
int n,t;
pair<int, int> a[N];
bool cmp ( pair<int, int> x,pair<int, int> y )
{
if ( x.first==y.first )
return x.second>y.second;
return x.first<y.first;
}
//template < class T> void read ( T &x ){ char c;int sign=1;x=0;while ( c=getchar(),(c>'9'||c<'0')&&c!='-') ;if ( c=='-' ){sign=-1;}else x=c-'0';while ( c=getchar(),c<='9'&&c>='0' ){x=x*10+c-'0';}x=x*sign;}
int main ( )
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int i,j;
int cnt,ss;
while ( cin>>n>>t )
{
int s,e,te;
for ( i=0;i<n;i++ )
{
cin>>s>>e;
a[i].first =s;
a[i].second =e;
}
sort ( a,a+n,cmp);
cnt=0;
te=0 ;
i=0;
while ( i<n )
{
j=i;
if ( te>=t ) //te>=t就可以退出了,这个细节让我错了十几次
break;
while ( te+1>=a[j].first&&j<n ){ // 区间 [1,2],[3,4]这样的也可以,所以te+1>...
++j;
}
if ( i<j )
{
++cnt;
while ( i<j ) //选择相邻交错区间中end最大的更新te,这样覆盖的范围最广
te=max ( te,a[i++].second );
}
else
break;
}
//printf ( " te:%d ss:%d\n",te,ss );
if ( te>=t&&a[0].first ==1 )
printf ( "%d\n",cnt);
else
printf ( "-1\n");
}
return 0;
}