您的位置:首页 > 其它

poj 2376 Cleaning Shifts 【贪心+快排】

2015-10-23 22:47 441 查看
Cleaning Shifts

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 14030Accepted: 3605
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.
Source

USACO 2004 December Silver

代码:

//题意:给定范围的一个区间,然后再给你若干个子区间,然后让你用这若干个子区间
//来填冲这个区间,用最少的子区间,来将这个大区间完全填冲,求最少需要多少个
//子区间!
//做法:首先,先将区间首段从小到大进行排序,如果首段相同,将按照尾端从大到小排序
//然后,在将首端在这个首端到尾端这个范围内找尾端最大值,然后替换原来的尾端,如果找到的尾端和原来的
//相同,那么说明不存在充满整个大区间的子区间,输出-1,否则继续找,知道t>=m||i>=n为止!
//然后再判断你求出的t是否大于等于m,如果大于,输出ans,否则输出-1!
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n,m;
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
};
struct node
{
int begin,end;
}a[25005];
int cmp(node a,node b)
{
if(a.begin==b.begin)
return a.end>b.end;
return a.begin<b.begin;
};
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].begin,&a[i].end);
}
sort(a,a+n,cmp);
if(a[0].begin!=1)
{
printf("-1\n");
continue;
}
int t=a[0].end;
int i=0,j=0;
int ans=1;
int maxn=t;
while(t<m&&i<n)
{
maxn=t;
while(a[i].begin<=t+1&&i<n)
{
maxn=max(maxn,a[i++].end);
}
if(maxn==t) break;
ans++;//用来保存需要的子区间的个数
t=maxn;//用来保存挑选的子区间的最右边的端点
}
if(t>=m)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: