Cleaning Shifts POJ - 2376(区间覆盖,排序+贪心)
2015-10-28 23:10
295 查看
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.
* 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.
The sample input
3 to 10
1 7
3 to 6
6 to 10
Sample output
2
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.
* 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.
The sample input
3 to 10
1 7
3 to 6
6 to 10
Sample output
2
/* * main.cpp * * Created on: 2015年10月21日 * Author: chen */ #include<stdio.h> #include<iostream> #include<string> #include<string.h> #include<algorithm> #include<vector> #include<time.h> #include<queue> #include<stack> #include<iterator> #include<math.h> #include<stdlib.h> #include<limits.h> #include<memory.h> //#define ONLINE_JUDGE #define eps 1e-8 #define INF 0x7fffffff //INT_MAX #define inf 0x3f3f3f3f //int?????????????????? #define FOR(i,a) for((i)=0;i<(a);(i)++) //[i,a); #define MEM(a) (memset((a),0,sizeof(a))) #define sfs(a) scanf("%s",a) #define sf(a) scanf("%d",&a) #define sfI(a) scanf("%I64d",&a) #define pf(a) printf("%d\n",a) #define pfI(a) printf("%I64d\n",a) #define pfs(a) printf("%s\n",a) #define sfd(a,b) scanf("%d%d",&a,&b) #define sft(a,b,c)scanf("%d%d%d",&a,&b,&c) #define for1(i,a,b) for(int i=(a);i<b;i++) #define for2(i,a,b) for(int i=(a);i<=b;i++) #define for3(i,a,b)for(int i=(b);i>=a;i--) #define MEM1(a) memset(a,0,sizeof(a)) #define MEM2(a) memset(a,-1,sizeof(a)) #define LL __int64 const double PI = acos(-1.0); template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template<class T> inline T Min(T a, T b) { return a < b ? a : b; } template<class T> inline T Max(T a, T b) { return a > b ? a : b; } using namespace std; template<class T> T Mint(T a, T b, T c) { if (a>b) { if (c>b) return b; return c; } if (c>a) return a; return c; } template<class T> T Maxt(T a, T b, T c) { if (a>b) { if (c>a) return c; return a; } else if (c > b) return c; return b; } const int maxn=25010; int T,n,m; struct node{ int x,y; }a[maxn]; bool cmp(node a,node b){//贪心排序,起点从小到大,若相同,终点从小到大 return a.x<b.x||(a.x==b.x&&a.y<b.y); } int work(){ int tmp,end=0,index=0,num=0; while(end<m&&index<n){//最远节点小于终点并且还有奶牛剩余 num++; tmp=end; if(a[index].x>end+1){//无法全部填充所有节点 return -1; } while(index<n&&a[index].x<=end+1){//查找当前节点所能到达的最远节点 tmp=max(tmp,a[index].y); index++; } end=tmp;//更新节点 } if(end<m)//若最远节点小于终点,则失败 return -1; return num; } int main() { #ifndef ONLINE_JUDGE freopen("test.in","r",stdin); freopen("test.out","w",stdout); #endif while(~sfd(n,m)){ for1(i,0,n) sfd(a[i].x,a[i].y); sort(a,a+n,cmp); printf("%d\n",work()); } return 0; }
相关文章推荐
- 巧建Spring Web的Maven项目来解决java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
- kali——安装nessus
- Android摇一摇功能实现
- js小案例效果学习记录
- 线段树的构造
- commons.logging 和 log4j
- vimrc
- yii2视频教程
- TOP 10 开源的推荐系统简介
- UiDevice的UiObject的讲解
- 一道题(1)
- 第一篇博客-初入Android解决Fetching Android SDK component information问题
- 小白笔记--------------------链栈
- 大三CS狗一点想法
- 怎样在spring初始化完成后执行一些操作?
- List集合拆分
- LightOJ - 1068 Investigation(数位DP)
- dropzone
- USB鼠标通信协议格式
- zoj3911 线段树区间修改与求素数的结合