贪心题,还可以,前段时间做的很多贪心题,所以感觉今天的挺简单的,不过,一开始cmp写错了,然后段错误一发(hhh)
解题思路:
- 关键是如何排序吧,是按照 l 从小到大即可。
- 然后我们只需要找出他们之间的空隙,也就是 左端点与当前最大的 x 之间的差值。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 100010;
struct node
{
int l, r;
}a[N];
bool cmp(node x, node y){
return x.l < y.l;
}
int main(){
int n, m;
scanf("%d%d",&n,&m);
int x = 1;
for (int i = 0; i < m;i ++){
int l, r;
scanf("%d%d",&l,&r);
a[i].l = l;
a[i].r = r;
}
sort(a,a + m, cmp);
int res = 0;
for (int i = 0; i < m; i ++){
res = max(res,a[i].l - x);
x = max(x,a[i].r);
}
res = max(res, n - x);
printf("%d\n",res);
return 0;
}