Clique Problem Solving Report

Source: Internet
Author: User

Clique Topic description

There are \ (n\) points on the number axis, the coordinates of the i\ points are \ (x_i\), and the weights are \ (w_i\). Two points \ (i\),\ (j\) exist between an edge and only if \ (ABS (X_i-x_j) >=w_i+w_j\). You need to ask for the maximum number of points in this chart. (a group is a set of vertices with edges between 22)

Input data

The first line is an integer \ (n\), and the next \ (n\) line is two integers per line ( x_i,w_i\).

Output data

An integer on one line indicates the answer

Data range

For \ (20\%\) data, \ (n<=10\).
For \ (60\%\) data, \ (n<=1000\).
For \ (100\%\) data, \ (n<=200000\), \ (0<=|x_i|,w_i<=10^9\).

A nature was found during the examination

If three points are left-to-right, point 1 points 2 have edges and point 2 points 3 to the right you can get points 1 points 3 have edges

So can \ (n^2\) run topo the longest road (from left to right with a forward edge)

Want to get (set\) optimize the edge but didn't think.

The result is a small explosion of 30.

The positive solution is not thought of it.

If a point is considered as an interval of the form \ ([x-w,x+w]\) , then the two points have a connecting edge equivalent to the interval no intersection.

So the problem turns into the number of intervals that don't overlap.

Can be simple greedy, can also DP

The complexity is \ (O (NLOGN) \)

Code:

#include <cstdio>#include <algorithm>const int N=2e5+10;struct node{    int l,r;    bool friend operator <(node n1,node n2){return n1.r<n2.r;}}seg[N];int rr[N],dp[N],f[N],pos[N],n;int max(int x,int y){return x>y?x:y;}int main(){    scanf("%d",&n);    for(int x,w,i=1;i<=n;i++)    {        scanf("%d%d",&x,&w);        seg[i].l=x-w,seg[i].r=x+w;        rr[i]=seg[i].r;    }    std::sort(seg+1,seg+1+n);    std::sort(rr+1,rr+1+n);    rr[0]=seg[1].r-1;    for(int i=1;i<=n;i++)    {        int l=0,r=i-1;        while(l<r)        {            int mid=l+r+1>>1;            if(seg[i].l>=rr[mid])                l=mid;            else                r=mid-1;        }        pos[i]=l;    }    for(int i=1;i<=n;i++)        dp[i]=f[pos[i]]+1,f[i]=max(f[i-1],dp[i]);    printf("%d\n",f[n]);    return 0;}

2018.10.13

Clique Problem Solving Report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.