poj 2376 Cleaning Shifts 【贪心+快排】
来源:互联网 发布:编程让系统死机 编辑:程序博客网 时间:2024/06/18 07:42
Cleaning Shifts
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14030 Accepted: 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.
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.
* 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 101 73 66 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.
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);elseprintf("-1\n");}return 0;}
0 0
- poj 2376 Cleaning Shifts 【贪心+快排】
- poj 2376 Cleaning Shifts( 贪心 )
- POJ 2376 Cleaning Shifts 贪心
- poj 2376 Cleaning Shifts( 贪心 )
- poj 2376 Cleaning Shifts 贪心
- 贪心-poj 2376-Cleaning Shifts
- POJ 2376 - Cleaning Shifts(贪心)
- Cleaning Shifts - POJ 2376 贪心
- Cleaning Shifts (poj 2376 贪心)
- Cleaning Shifts - POJ 2376 贪心
- [贪心] poj 2376 Cleaning Shifts
- [贪心] poj 2376 Cleaning Shifts
- POJ 2376 Cleaning Shifts [贪心]
- Poj 2376 Cleaning Shifts【贪心】
- POJ 2376 Cleaning Shifts 贪心
- POJ 2376 Cleaning Shifts(贪心)
- POJ 2376 Cleaning Shifts 贪心
- POJ 2376 Cleaning Shifts(贪心)
- TCP协议的三次握手建立连接及四次握手断开连接
- C语言运算符的优先级和关键字
- UVA 11464 偶数矩阵
- class命名规则
- python list的append和extend操作
- poj 2376 Cleaning Shifts 【贪心+快排】
- iOS生命周期简单演示
- Rhapsody软件框架和UML的实时系统开发
- 剑指Offer--第一个只出现一次的字符位置
- pycharm-professional-4.5.4 注册码 破解
- 使用Eclipse配置Struts2开发环境
- Entity Framework4.1实现动态多条件查询、分页和排序
- TimesTen临时(内存)空间使用和调整临时(内存)空间
- 在使用SimpleJdbcDaoSupport作为基类创建Dao时候出现的奇怪的BUG