OpenJ_Bailian 2376

Cleaning Shifts

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status Practice OpenJ_Bailian 2376

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

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.

 

题意:

       FJ有N头牛,希望安排它们在时刻1到时刻T进行工作。每头牛都只能在它自己的一个特定工作的时间段(从时刻i到时刻j)进行工作。FJ想知道至少安排多少牛进行工作才能保证时刻1到T的任意时刻都会有至少一头牛在工作。

 

输入:

       第一行N和T,之后N行给出每头牛的工作时间段。

输出:

       至少安排的牛的头数。如果不存在方案则输出-1。

 

分析:

       使用贪心策略,对每头牛按其时间段进行排序。由于需要覆盖整个时间区间,所以先按牛时间段的起始时间进刻由小到大进行排序。又由于需要minimize牛的数量,所有在之前排序的基础上再对牛时间段的结束时刻由大到小进行排序。然后遍历一遍排序好的牛区间就可以了。maxi变量来保存最大的右边界,tmp变量保存更新之后的当前右边界。对于当前的右边界tmp,不断向后遍历左边界不大于tmp+1的牛区间,用这些牛区间右边界最大值去更新maxi的值,如果这个值小于T,那么将tmp置为maxi,持续进行这个过程。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <vector>
 5 #include <string>
 6 #include <algorithm>
 7 #include <cmath>
 8 using namespace std;
 9 const int MAX_N = 25000;
10 int N,T;
11 struct node{int st,ed;}n[MAX_N + 10];
12 bool cmp(const node& n1,const node& n2){
13     if(n1.st == n2.st) return n1.ed > n2.ed;
14     else return n1.st < n2.st;
15 }
16 int main(){
17     int ans = 1;
18     scanf("%d%d",&N,&T);
19     for(int i = 0 ; i < N ; i++)
20         scanf("%d%d",&n[i].st,&n[i].ed);
21     sort(n,n + N,cmp);
22     if(n[0].st > 1) {
23         printf("-1\n");
24         return 0;
25     }
26     if(n[0].ed == T){
27         printf("1\n");
28         return 0;
29     }
30     int tmp = n[0].ed;
31     int maxi = tmp;
32     for(int i = 1 ; i < N ; i++){
33         if(n[i].st > tmp + 1){
34             ans++;
35             tmp = maxi;
36         }
37         if(n[i].st <= tmp + 1){
38             maxi = max(maxi,n[i].ed);
39             if(n[i].ed == T){
40                 ans++;
41                 tmp = T;
42                 break;
43             }
44         }
45     }
46     if(tmp == T){
47         printf("%d\n",ans);
48     }
49     else printf("-1\n");
50     return 0;
51 }
View Code

 

转载于:https://www.cnblogs.com/cyb123456/p/5783136.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我! 毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip毕设新项目-基于Java开发的智慧养老院信息管理系统源码+数据库(含vue前端源码).zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值