poj2376

排序+贪心

View Code
#include <iostream>
#include
<cstdlib>
#include
<cstring>
#include
<cstdio>
#include
<algorithm>
using namespace std;

#define maxn 25005

struct Interval
{
int s, e;
}interval[maxn];

int n, t;

bool operator < (const Interval &a, const Interval &b)
{
if (a.s != b.s)
return a.s < b.s;
return a.e < b.e;
}

int main()
{
//freopen("t.txt", "r", stdin);
scanf("%d%d", &n, &t);
for (int i = 0; i < n; i++)
scanf(
"%d%d", &interval[i].s, &interval[i].e);
sort(interval, interval
+ n);
int temp = 0;
int ans = 0;
int i = 0;
while (temp < t && i < n)
{
ans
++;
int top = temp;
if (interval[i].s > temp + 1)
{
printf(
"-1\n");
return 0;
}
while (i < n && interval[i].s <= temp + 1)
{
top
= max(top, interval[i].e);
i
++;
}
temp
= top;
}
if (temp < t)
printf(
"-1\n");
else
printf(
"%d\n", ans);
return 0;
}

你可能感兴趣的:(poj)