zoukankan      html  css  js  c++  java
  • CF #296 (Div. 1) B. Clique Problem 贪心(构造)

    B. Clique Problem
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.

    Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.

    Find the size of the maximum clique in such graph.

    Input

    The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.

    Each of the next n lines contains two numbers xiwi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.

    Output

    Print a single number — the number of vertexes in the maximum clique of the given graph.

    Sample test(s)
    input
    4
    2 3
    3 1
    6 1
    0 2
    output
    3

    题目意思:

    n个点,每个点有x和w,若两个点i和j满足|xi-xj|>=wi+wj,则两个点连边,求最大团。

    思路:
    每个点可以构造线段[xi-wi,xi+wi],若两个线段不想交则满足不等式,那么问题转换为n条线段,求最大的集合使得线段两两不想交,按线段右端点从小到大排序贪心。

    代码:

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 #include <vector>
     6 #include <queue>
     7 #include <cmath>
     8 #include <set>
     9 using namespace std;
    10 
    11 #define N 200005
    12 
    13 struct node{
    14     int l, r;
    15 }a[N];
    16 
    17 bool cmp(node a,node b){
    18     if(a.r==b.r) return a.l>b.l;
    19     return a.r<b.r;
    20 }
    21 
    22 int n;
    23 
    24 main()
    25 {
    26     int i, j, k;
    27     cin>>n;
    28     for(i=0;i<n;i++){
    29         scanf("%d %d",&j,&k);
    30         a[i].l=j-k;
    31         a[i].r=j+k;
    32     }
    33     sort(a,a+n,cmp);
    34     int ans=1;j=a[0].r;
    35     for(i=1;i<n;i++){
    36         if(a[i].l>=j){
    37             ans++;
    38             j=a[i].r;
    39         }
    40     }
    41     printf("%d
    ",ans);
    42 }
  • 相关阅读:
    OPC-UA和IEC 62541协议
    excel多级部门字符串截取其中一端的公式
    mac 显示音频文件 速率
    解决:Mac安装HOME brew 拒绝了我们的连接请求解决方案
    ss自定义规则
    macos关闭更新功能
    mac 使用触摸板左键长按选择是,总是弹出系统自带词典的问题
    固定区域截图快速粘贴到表格
    Outlook 2016 for Mac 更改邮件存放路径
    微信发送高清视频(避免被微信压缩变模糊),100M以内
  • 原文地址:https://www.cnblogs.com/qq1012662902/p/4805289.html
Copyright © 2011-2022 走看看