• [codeforces 528]B. Clique Problem


    [codeforces 528]B. Clique Problem

    试题描述

    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.

    输入

    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.

    输出

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

    输入示例

    4
    2 3
    3 1
    6 1
    0 2

    输出示例

    3

    数据规模及约定

    见“输入

    题解

    把节点 i 转化成线段 [xi - wi, xi + wi],然后题目求的就是没有交集的最多的线段条数。贪心即可。

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <stack>
    #include <vector>
    #include <queue>
    #include <cstring>
    #include <string>
    #include <map>
    #include <set>
    using namespace std;
    
    const int BufferSize = 1 << 16;
    char buffer[BufferSize], *Head, *Tail;
    inline char Getchar() {
        if(Head == Tail) {
            int l = fread(buffer, 1, BufferSize, stdin);
            Tail = (Head = buffer) + l;
        }
        return *Head++;
    }
    int read() {
        int x = 0, f = 1; char c = getchar();
        while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
        while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
        return x * f;
    }
    
    #define maxn 200010
    #define LL long long
    int n, f[maxn];
    struct Point {
    	int x, v;
    	Point(): x(0), v(0) {}
    	Point(int _, int __): x(_), v(__) {}
    	bool operator < (const Point& t) const { return x + v < t.x + t.v; }
    } ps[maxn];
    
    int main() {
    	n = read();
    	for(int i = 1; i <= n; i++) ps[i].x = read(), ps[i].v = read();
    	
    	sort(ps + 1, ps + n + 1);
    	for(int i = 1; i <= n; i++) {
    		int x = upper_bound(ps + 1, ps + n + 1, Point(ps[i].x, -ps[i].v)) - ps - 1;
    		f[i] = max(f[i-1], f[x] + 1);
    	}
    	
    	printf("%d
    ", f[n]);
    	
    	return 0;
    }
    
  • 相关阅读:
    JVM 启动参数设置
    Linux文件分割与合并
    设置tomcat使用指定的jdk版本
    java字符编码
    HASH 哈希处理完数据导致数据集第一行数据缺失
    HASH 何时将key加载到h.definedata()中
    字符串 批量全角、半角转换
    SAS_正则表达式 字符意义
    正则表达式基础篇
    sas options有用的全局设置
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/5810633.html
Copyright © 2020-2023  润新知