【51nod 1563】 坐标轴上的最大团 (贪心)

Description

坐标轴上有n个点,每个点有一个权值。第i个点的坐标是 xi x i ,权值是 wi w i 。现在对这些点建图。对于点对 (i,j) ( i , j ) ,如果 |xixj|wi+wj | x i − x j | ≥ w i + w j ,那么就给第 i i 个点和第j个点之间连一条边。问建好的图中最大团有几个点。

Solution

可以发现其实每一个点可以看做以 xi x i 为中心的,长度为 2×wi 2 × w i 的线段。问题就变成了在这n条线段中选尽量多的线段使得线段两两不相交,然后就可以先将有包含关系的线段中的较长线段去掉,因为它肯定不会成为最优解。然后就可以贪心了。

Code

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#define For(i , j , k) for (int i = (j) , _##end_ = (k) ; i <= _##end_ ; ++ i)
#define Fordown(i , j , k) for (int i = (j) , _##end_ = (k) ; i >= _##end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define INF (0x3f3f3f3f)
#define Mod (1000000007)
using namespace std;
typedef long long LL;
typedef pair<int , int> PII;

template <typename T> inline bool chkmax(T &a , T b) { return a < b ? (a = b , 1) : 0; }
template <typename T> inline bool chkmin(T &a , T b) { return b < a ? (a = b , 1) : 0; }

int _ , __;
char c_;
inline int read()
{
    for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

inline void file()
{
#ifndef ONLINE_JUDGE
    freopen("51nod1563.in" , "r" , stdin);
    freopen("51nod1563.out" , "w" , stdout);
#endif
}

const int maxn = 200010;

struct Line
{
    int l , r;
    bool operator < (const Line &line) const
    {
        return l < line.l;
    }
}L[maxn];

int n , m , xi , wi , last , cnt;

int main()
{
    file();
    n = read();
    For(i , 1 , n)//转化成线段
    {
        xi = read();
        wi = read();
        L[i].l = xi - wi;
        L[i].r = xi + wi;
    }
    sort(L + 1 , L + 1 + n);//按左端点排序
    for (int i = 1 ; i <= n ; ++ i)//去包含关系
    {
        while (m && L[m].r > L[i].r)
            -- m;
        L[++ m] = L[i];
    }
    last = -2000000001;
    for (int i = 1 ; i <= m ; ++ i)//贪心
        if (last <= L[i].l)
        {
            last = L[i].r;
            ++ cnt;
        }
    printf("%d\n" , cnt);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值