http://poj.org/problem?id=2376
给定N个小区间以及区间起点终点,求能用它们覆盖区间[1,T]的最小组合。
从左往右,尽量选择长度最大的区间。
首先对所有奶牛按照开始时间排序。
然后更新起点=终点+1,搜索剩下的奶牛中能够覆盖这个起点同时终点最远的那一头,更新终点。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
int n,t;
struct E{
int l,r;
}e[25005];
bool cmp(struct E a,struct E b){
return a.l<b.l;
}
int main(){
cin >> n >> t;
for (int i=0;i<n;i++){
cin >> e[i].l >> e[i].r;
}
sort(e,e+n,cmp);
int Left=1;
int Right=0;
int ans=0;
int i=0;
while (Left<=t){
while (i<n&&e[i].l<=Left){
if (e[i].l<=Left&&e[i].r>Right){
Right=e[i].r;
}
i++;
}
if (Right>=Left){
Left=Right+1;
ans++;
}
else break;
}
if (Left<=t) cout << "-1" << endl;
else cout << ans << endl;
}