Description
给一个1..T的区间,然后N个子区间,取最少的把这个区间填满,求这个最小值
Algorithm
以区间开始为关键字升序排序,如果左边不能覆盖,那肯定就不行。这样就选好了最开始的一块。那当然是选所以1开头的右边区间最大的一块。第二块就是在这1..y+1中再找个右边最大的一块,然后不断更新。如果找不到,那就不能填满,当全部找完以后,再判断能不能填满
Code
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 20000;
class V
{
public:
int x, y;
};
bool compare(V a, V b)
{
if (a.x < b.x) return true;
if (a.x > b.x) return false;
if (a.x == b.x)
if (a.y > b.y) return true;
return false;
}
int main()
{
int n, t;
scanf("%d%d", &n, &t);
V a[maxn];
for (int i=0;i<n;i++)
{
int x, y;
scanf("%d%d", &x, &y);
a[i].x = x;
a[i].y = y;
}
sort(a, a+n, compare);
if (a[0].x!=1)
{
puts("-1");
return 0;
}
int last = a[0].y;
int last_max = last;
int i = 0;
int ans = 1;
do
{
if (last >= t) break;
if (a[i].x>last+1)
{
ans = -1;
break;
}
while (a[i].x<=last+1 && i<n)
{
if (a[i].y > last_max) last_max = a[i].y;
i++;
}
last = last_max;
ans++;
}while(i<n);
if (last_max < t) ans = -1;
printf("%d", ans);
return 0;
}
新技能GET
用sort函数进行多关键字排序
生词
- assign 分配
- chore 家务
- barn 谷仓
- interval 区间