C语言--Cleaning shifts
来源:互联网 发布:墙壁网络水晶头接线图 编辑:程序博客网 时间:2024/05/22 10:52
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.
题目大意:就是给出了奶牛的头数N,还有总工作时间T,下面有N头奶牛工作的时间区间,输出能达到这个效果的最少奶牛个数。如果没有输出-1.
题目分析:很明显又是结构体,然后贪心算法,但很明显有一点区别,需要从一开始
重点及注意事项:1.从一开始,尽可能选大的区间,然后与下一个尽可能大的数据结合分析
2.如何将两个区间合成一个新区间
3.如何判断条件在什么情况下成立分析(看着代码说吧)
AC源代码
#include <stdio.h>#include <algorithm>using namespace std;struct point{int start;int end;}cow[250001];bool cmp(point a, point b){if (a.start==b.start)return a.end>b.end;return a.start<b.start;//对开始时间升序,结束时间降序排列}//这样可以保证找到的每一个区间足够的大int main(){int n,t;scanf("%d%d",&n,&t);for(int i=0;i<n;i++)scanf("%d%d",&cow[i].start,&cow[i].end);sort(cow,cow+n,cmp);//结构体排序if(cow[0].start==1){if (cow[0].end==t)printf("1\n");else{int j=0;int ans=1;int start,end;bool find=false;start=end=cow[0].end;while (!find){for(int i=j+1;i<n;i++){if(cow[i].start-1<=start&&cow[i].end>end){end=cow[i].end;j=i;//两个满足条件区间合成一个新区间}}if(start==end){break;}else if(end==t){find=true;}ans++;start=end;//定义变量find,查找符合条件的区间}if(find=true){printf("%d\n",ans);}else{printf("-1\n");}}}else{printf("-1\n");}return 0;}
附上运行截图
0 0
- C语言--Cleaning shifts
- Cleaning Shifts
- Cleaning Shifts
- Cleaning Shifts
- - Cleaning Shifts
- Cleaning Shifts
- Cleaning Shifts
- PKU 2376 Cleaning Shifts
- POJ 2376 Cleaning Shifts
- Cleaning Shifts(p2376)
- POJ3171 Cleaning Shifts 数据结构
- POJ 2376 Cleaning Shifts
- Pku2376 Cleaning Shifts
- poj2376 Cleaning Shifts
- poj 2376 Cleaning Shifts
- POJ-2376-Cleaning Shifts
- poj 2376 Cleaning Shifts
- POJ 2376 Cleaning Shifts
- ZOJ - 1579(贪心)
- andengine源码及示例程序
- linux 线程学习之条件变量
- jQuery取得select选择的文本与值
- Eclipse+tomcat开发j2ee经典入门例子
- C语言--Cleaning shifts
- 嵌入式软件开发---多家企业面经合集
- PAT basic1026-1030(Python版)
- QT QSqlQuery QSqlQueryModel
- POJ2352(树状数组)
- Android Push实现总结
- android webview开发html5之二
- RabbitMQ学习之spring整合发送同步消息(注解实现)
- java异常处理题目