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.
等等。。。
本题是给定一个n表示有n个数据吧区间1~T填满,要求使用最小
解法:贪心大法,对于所给的数据进行排序,按照开始时间从小到大排序
用一个ftime来记录结束时间,最后走动啊time>T。
用一个flag来记录每一个小区间的最大值。
不要忘记如果最开始不是1 就是输出-1不要犹豫
以下是我的代码
#include <vector>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <cstdio>
#include <algorithm>
using namespace std;
struct sa{
int x,y;}a[25010];
int cmp(const sa a,const sa b){
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
int main(){
int n,t;
cin>>n>>t;
for(int i=0;i<n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
sort(a,a+n,cmp);
if(a[0].x!=1){
cout<<-1<<endl;
return 0;
}//直接输出-1
int time=0;
int k=0;
for(int i=0;i<n;i++){
if(a[i].x!=1){
time=a[i-1].y;
k=i;
break;
}
}//用k来找1的最后一个
// cout<<k<<" :"<<time<<endl;
if(k==0){
cout<<1<<endl;
return 0;
}
// 第一个就到最后
if(a[k-1].y>=t){
cout<<1<<endl;
return 0;
}//1的区间就到尾了
int flag=0;
int sum=1;
for(int i=k;i<n;i++){
if(a[i].x<=time+1){
flag=max(flag,a[i].y);
}
if(a[i+1].x>time+1){
time=max(time,flag);
flag=0;
sum++;
// i--;
}
if(i==n-1){
time=max(time,flag);
sum++;
}
if(time>=t){
break;
}
}
// cout<<time;
if(time<t)
cout<<-1<<endl;
else cout<<sum<<endl;
return 0;
}