51Nod-1563-坐标轴上的最大团

来源:互联网 发布:淘宝客服的服务用语 编辑:程序博客网 时间:2024/05/20 21:57

ACM模版

描述

描述

题解

这个题根据题意,我们知道,根据 xw 可以确定某一个点的不可连边的区间,而两个点的区间只要不重叠,就可以连边,那么最大团就是尽可能多的选取互相不重叠的区间,也就变成了类似于 01 背包的问题,可是这个题好像数据比较弱还是怎么回事,直接排序后贪心就能过……所以,水过了!!!

代码

#include <cstdio>#include <iostream>#include <algorithm>using namespace std;const int MAXN = 2e5 + 10;const int INF = 0x3f3f3f3f;struct node{    int l, r;};int n;node a[MAXN];bool cmp(node a, node b){    if (a.r == b.r)    {        return a.l < b.l;    }    return a.r < b.r;}int main(){    cin >> n;    int x, w;    for (int i = 0; i < n; i++)    {        scanf("%d%d", &x, &w);        a[i].l = x - w;        a[i].r = x + w;    }    sort(a, a + n, cmp);    int L = -INF;    int ans = 0;    for (int i = 0; i < n; i++)    {        if (a[i].l >= L)        {            L = a[i].r;            ans++;        }    }    cout << ans << endl;    return 0;}
原创粉丝点击