Analytical Formula Codeforces 528B clique problem

Source: Internet
Author: User

Http://codeforces.com/contest/528/problem/b

Clique Problemtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard 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 graphG. It is required to find a subset of verticesCOf the maximum size such that any of the them is connected by a edge in graphG. Sounds simple, doesn ' t it? Nobody yet knows an algorithm, 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 are easier if you consider a specific type of a graph.

ConsiderNDistinct points on a line. Let theI-th Point has the coordinate xi and weight wi . Let ' s form graphG, whose vertices is these points and edges connect exactly the pairs of points(i,? J), such that the distance between them are not less than the sum of their weights, or more formally:| x I?-? x J|? ≥? W i? +? W J .

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 nextNLines contains, numbers xi , wi (0?≤? x i? ≤?109,? 1?≤? W i? ≤?109 )-the coordinate and the weight of a point. All xi is different.

Output

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

Sample Test (s) input
42 33 16) 10 2
Output
3
Note

If you happen to know what to solve this problem without using the specific properties of the graph formulated in the PROBL EM statement, then you is able to get a prize of one million dollars!

The picture for the sample test.



Direct Analysis Formula AH Ah ah Ah!!!

Test instructions

N points on a given axis.

The following n lines are two numbers per line XI, WI represents point and point weights.

For any two points u, v

If Dis (u,v) >= u_w+v_w, one side can be built between the two points. (In the other words if the distance between two points is greater than two points of weight and can be built side)

Find a maximum regiment and output the maximum number of points.



#include <bits/stdc++.h> #define LL intusing namespace Std;pair<ll,ll>p[200005];int main () {ll n,i,w,x,s=0, l=-(1<<31); Cin>>n;for (i=0;i<n;i++) Cin>>x>>w,p[i]=make_pair (x+w,x-w); sort (p,p+n); for (i=0;i<n;i++) if (p[i].second>=l) l=p[i].first,s++;cout<<s;}

By the way, classmate with DP, segment tree or tree-like array!

Actually, for a weighted point, we can think of it as an interval.

such as: 4 5, can be considered as the interval [-1, 9]

Then this point can be transferred from the interval (-inf, 1], i.e. val (9) = Max (Val (9), Interval (-inf,-1] max. +1)

Then the front interval maximum is maintained with a tree array or a segment tree

#include <stdio.h> #include <string.h> #include <iostream> #include <math.h> #include <queu  e> #include <set> #include <vector> #include <string> #include <algorithm> template <class    t>inline BOOL Rd (T &ret) {char c; int sgn;    if (c = GetChar (), c = = EOF) return 0;    while (c! = '-'-' && (c< ' 0 ' | | c> ' 9 ')) C = GetChar (); SGN = (c = = '-')?    -1:1; ret = (c = = '-')?    0: (C-' 0 ');    while (c = GetChar (), C >= ' 0 ' &&c <= ' 9 ') ret = ret * + (C-' 0 ');    RET *= SGN; return 1;}        Template <class t>inline void pt (T x) {if (x <0) {Putchar ('-');    x =-X;    } if (X>9) pt (X/10); Putchar (x 10 + ' 0 ');} using namespace Std;const int N = 400010; #define L (x) tree[x].l#define R (x) tree[x].r#define Max (x) tree[x].max#define Lso N (x) (x<<1) #define Rson (x) (x<<1|1) #define LAZY (x) tree[x].lazystruct node{int L, R, Max, lazy;} tree[n<<2];void down (int id) {MAX (Lson (id)) = MAX (Lazy (ID), MAX (Lson (ID)));    Max (Rson (id)) = MAX (Lazy (ID), MAX (Rson (ID)));    Lazy (Lson (id)) = max (lazy (id), lazy (Lson (ID))); Lazy (Rson (id)) = max (lazy (id), lazy (Rson (ID)));} void U (int id) {max (id) = MAX (max (Lson (ID)), max (Rson (ID)));} void build (int l, int r, int id) {L (id) = l;    R (ID) = R;    Max (id) = Lazy (id) = 0;    if (L = = r) return;    int mid = (L + r) >> 1; Build (L, Mid, Lson (ID)); Build (mid + 1, R, Rson (ID));}    int query (int l, int r, int id) {if (L = = L (ID) && r (id) = = r) return Max (ID);    Down (ID);    int mid = (L (ID) + R (ID)) >> 1, ans;    if (R <= mid) ans = query (l, R, Lson (ID));    else if (Mid < L) ans = query (l, R, Rson (ID));    else ans = max (query (L, Mid, Lson (ID)), query (mid + 1, R, Rson (ID)));    U (ID); return ans;}        void up (int l, int r, int val, int id) {if (L = = L (ID) && r (id) = = r) {max (id) = MAX (max (ID), Val); Lazy (id) = MAX (lazy (id), Val);       Return    } down (ID);    int mid = (L (ID) + R (ID)) >> 1;    if (R <= mid) up (L, R, Val, Lson (id));    else if (Mid < L) Up (L, R, Val, Rson (id));        else {up (L, Mid, Val, Lson (id));    Up (mid + 1, R, Val, Rson (id)); } U (ID);} Vector<int>g;int hehehe;struct edge{int L, R;} X[n];bool CMP (Edge A, Edge b) {return A.R < B.R;}        int n, M;int a[n], B[n];int main () {while (~SCANF ("%d", &n)) {g.clear ();            for (int i = 1; I <= n; i++) {rd (a[i]); Rd (B[i]);            X[I].L = A[i]-b[i];            X[I].R = A[i] + b[i];            G.push_back (X[I].L);        G.push_back (X[I].R);        } sort (G.begin (), G.end ());        G.erase (Unique (G.begin (), G.end ()), G.end ());            for (int i = 1; I <= n; i++) {x[i].l = Lower_bound (G.begin (), G.end (), X[I].L)-G.begin () + 1;        X[I].R = Lower_bound (G.begin (), G.end (), X[I].R)-G.begin () + 1; } sort (x + 1, x + N + 1, CMP);        Build (1, G.size (), 1);        int ans = 1;            for (int i = 1; I <= n; i++) {int tmp = query (1, X[I].L, 1) + 1;            ans = max (ans, TMP);        Up (X[I].R, X[I].R, TMP, 1); } pt (ANS);    Puts (""); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Analytical Formula Codeforces 528B clique problem

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.