POJ 2376 Cleaning Shifts
来源:互联网 发布:shell编程入门书籍推荐 编辑:程序博客网 时间:2024/05/18 06:21
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.
题目大意:
有一些奶牛,每只奶牛负责一个时间段。问覆盖完全部的时间段最少需要多少只奶牛。若不能全部覆盖,输出-1.
解题思路:
先按照奶牛开始时间段按从小到大排序,用快排:
int cmp(time a,time b)
{
return a.x<b.x;
}
{
return a.x<b.x;
}
sort(a+1,a+n+1,cmp);
定义一个变量t用来记录已选定的奶牛的结束时间,选择起始时间在选定奶牛的时间段内且结束时间最晚的那只奶牛为当前选定奶牛,更新t,temp变量的值,最后判断t是否小于T。
代码:
#include<iostream>#include<stdlib.h>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct time{ int x; int y;} a[25001];int cmp(time a,time b){ return a.x<b.x;}int main(){ int i,n,T; scanf("%d%d",&n,&T); for (i=1; i<=n; ++i) scanf("%d%d",&a[i].x,&a[i].y); sort(a+1,a+n+1,cmp); a[n+1].x=0x7fffffff; int t=0,temp=0,ans=0; int f=0; for (i=1; i<=n; ++i) if (a[i].x<=t+1) { if(temp<a[i].y) { temp=a[i].y; f=1; } if(a[i+1].x>t+1 && f) { t=temp; ++ans; f=0; } } if (t<T) printf("-1\n"); else printf("%d\n",ans); 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
- poj 2376 Cleaning Shifts
- POJ 2376 Cleaning Shifts
- poj 2376 Cleaning Shifts
- POJ-2376 Cleaning Shifts
- POJ 2376Cleaning 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
- 基于伪代码编程
- HDU 1250: Hat's Fibonacci
- il语言
- 二叉树中和为某一值的路径(面试题 25)
- hdu3592 World Exhibition --- 差分约束
- POJ 2376 Cleaning Shifts
- 拨号上网
- WEB安全之CSRF攻击原理
- 湖南省第九届大学生计算机程序设计竞赛
- Charm Bracelet
- Circular RMQ
- 无线wifi
- Boost中文件系统类FileSystem全面解析
- Tomcat服务器的主要目录结构: