poj 2376 Cleaning Shifts 【贪心+快排】
2015-10-23 22:47
441 查看
Cleaning Shifts
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
Sample Output
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
代码:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14030 | Accepted: 3605 |
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; }
相关文章推荐
- 递归算法复杂度的求法
- xcode遇到的问题
- 读《UML大战需求分析》有感二
- 多线程(死锁)
- UVA 11464 偶数矩阵
- iOS-捕获应用程序崩溃日志
- EM3002D、Seapath200、MRU-H安装测量及校准
- Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能
- TCP协议的三次握手建立连接及四次握手断开连接
- Laravel入门基础教程(一)
- 包装类(转)
- 递归和for循环
- 欢迎使用CSDN-markdown编辑器
- HTML中下拉框的简单介绍<Select><option>
- android中的SurfaceView,SurfaceHolder,SurfaceHolder.CallBack
- 二,jquery对象 与 dom对象关系
- LeetCode OJ:Binary Search Tree Iterator(二叉搜索树迭代器)
- 编程思想之多线程与多进程(1)——以操作系统的角度述说线程与进程
- 如何从List集合中删除对象
- java基础的总结(1)