我真的是太菜了,这道题让我想了好久,最后看别人的解说才明白是区间覆盖问题。
源自poj2376
这道题就是一个套路。。。。
#include <iostream>
#include<cstdio>
#include <algorithm>
using namespace std;
const int MAX = 1e6 + 10;
struct P
{
int x, y;
};
int n, t;
P num[MAX];
bool cmp(P x, P y)
{
if (x.x == y.x) return x.y < y.y;
return x.x < y.x;
}
int main()
{
cin >> n >> t;
bool start = false, end = false;
for (int i = 1; i <= n; i++)
{
scanf_s("%d%d", &num[i].x, &num[i].y);
if (num[i].x == 1) start = true;
if (num[i].y == t)end = true;
}
sort(num + 1, num + n + 1, cmp);
if (!start || !end) { cout << -1 << endl; return 0; }
int pos = 1, tmp = 1, right = 0; int ans = 0;
while (true)
{
//cout << tmp << endl;
while (pos <= n && num[pos].x <= right + 1)
{
if (num[pos].y > tmp) tmp = num[pos].y; pos++;
}
if (right != tmp)
{
right = tmp; ans++;
if (right >= t) break;
}
else
{
cout << -1 << endl; return 0;
}
}
cout << ans << endl;
return 0;
}