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

来源:互联网 发布:淘宝流量有什么作用 编辑:程序博客网 时间:2024/06/05 15:45

Description

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

Solution

可以发现其实每一个点可以看做以xi为中心的,长度为2×wi的线段。问题就变成了在这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;}
原创粉丝点击