Cleaning Shifts / OpenJ_Bailian - 2376
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.
题意
给你N,T,再给你N个区间,求最少选择多少个区间能覆盖[1,T]之间所有的整数位置
N <= 25000, T <= 1000000
题解
先按区间左端点从小到大排序
从第一个区间开始,
每次找一个区间,使其左端点是<=当前区间右端点的最大端点
每次循环,区间数+1
代码
#include<bits/stdc++.h>
using namespace std;
int n, t;
struct node{
int left, right;
}a[25010];
bool cmp(node x, node y){
return x.left < y.left;
}
int main(){
scanf("%d%d", &n, &t);
for(int i = 1; i <= n; i++){
scanf("%d%d", &a[i].left, &a[i].right);
}
sort(a+1, a+n+1, cmp);//按区间左端点从小到大排序
int j = 1, r = 0, ret = 0;
for(int i = 1; i <= n; i = j){
int rm = 0;
while(j <= n && a[j].left <= r+1){//找j点使其左端点是 最大的 小于i点右端点的点
rm = max(rm, a[j++].right);
}
if(!rm) break;//没有这个点
r = rm;//更新下当前右端点
ret++;//区间数加1
if(rm >= t) break;//满足条件
}
if(r < t){
printf("%d\n", -1);
}
else printf("%d\n", ret);
return 0;
}
PS
水题,嗯
稍微具体分析
https://www.cnblogs.com/Little-Turtle--QJY/p/12390186.html
没有未来的未来不是我想要的未来