http://poj.org/problem?id=2376
题目大意:
给你一些区间的起点和终点,让你用最小的区间覆盖一个大的区间。
思路:
贪心,按区间的起点找满足条件的并且终点尽量大的。
一开始排序还考虑了起点一样终点要大的,想了想没必要,因为只后都是直接扫描满足条件的。。
注意的是比如【4.5】下一次可以直接【6,10】这样。。。这一步坑了我好久。。。
后来一直WA,改了老半天。。。发现是大于等于少写了个等号,,,我去面壁。。。我要蹲墙角。。。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=25000+10;
struct cow{
int s,e;//start time and end time
bool operator <(const cow &x)const{
// if(s==x.s) // 也没必要这么排序。。
// return e>x.e;
return s<x.s;
}
}a[MAXN];
int main()
{
int n,T;
while(~scanf("%d%d",&n,&T))
{
for(int i=0;i<n;i++)
scanf("%d%d",&a[i].s,&a[i].e);
sort(a,a+n);
int s=0,e=0; //s必须从0开始,因为下面判断是s+1
int ans=0;
for(int i=0;i<n;i++)
{
if(s>=T) break; //少写了个等于。改了一小时。。。。。我去面壁
bool find=false;
for(int j=i;j<n;j++) //找到满足条件的结束时间最大的
{
if(a[j].s>s+1) break;
if(a[j].e>e) {
e=a[j].e;
i=j;
}
find=true;
}
if(!find)
{
ans=-1;
break;
}
ans++;
s=e;
}
if(s<T) ans=-1;
printf("%d\n",ans);
}
return 0;
}