题意:给定n和T,n代表牛的头数,T代表要覆盖的区间1~T。
思路:贪心,区间覆盖问题。很久没写了,细节卡了我好久,忧伤!!
代码附上:
#include <cstdio>
#include <cstring>
#include <cctype>
#include <iostream>
#include <queue>
#include <set>
#include <cmath>
#include <algorithm>
#define FIN freopen("input.txt","r",stdin)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int MAX = 25005;
const int INF=0x3f3f3f3f;
struct node{
int s,e;
}rec[MAX];
int n,t;
bool cmp(node a,node b){
return a.s==b.s?a.e<b.e:a.s<b.s;
}
int main()
{
//FIN;
while(~scanf("%d%d",&n,&t)){
for(int i=0;i<n;i++){
scanf("%d%d",&rec[i].s,&rec[i].e);
if(rec[i].s>rec[i].e) swap(rec[i].s,rec[i].e);
}
sort(rec,rec+n,cmp);
if(rec[0].s>1) {printf("-1\n");continue;}
int start=1,end=0,res=1;
for(int i=0;i<n;i++){
if(rec[i].s<=start) {end=max(end,rec[i].e);if(end>=t)break;}
else {
start=end+1;
if(rec[i].s<=start) {res++;end=max(rec[i].e,end);if(end>=t)break;}
}
}
if(end>=t) printf("%d\n",res );
else printf("-1\n");
}
return 0;
}