B. Clique Problem(贪心)
题目链接:
2 seconds
256 megabytes
standard input
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.
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (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 题意:满足上面的式子的点对连一条边,问连完边后最大独立团的点数是多少;
思路:假设xi>=xj,那么xi-wi>=xj+wj,那么按x排序后,对于每一个点就可以与<=xi-wi区间的点相连(这些点区间假设为[l,r]),
那么[l,r]区间的最大团数目加1就可以更新当前点的值了;
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int n,dp[maxn];
std::vector<int> ve;
struct node
{
int x,w;
}po[maxn];
int cmp(node a,node b){return a.x<b.x;}
struct Tree
{
int l,r,mx;
}tr[4*maxn];
void build(int o,int L,int R)
{
tr[o].l=L;tr[o].r=R;tr[o].mx=1;
if(L>=R)return ;
int mid=(tr[o].l+tr[o].r)>>1;
build(2*o,L,mid);build(2*o+1,mid+1,R);
}
int query(int o,int L,int R)
{
if(L<=tr[o].l&&R>=tr[o].r)return tr[o].mx;
int ans=0;
int mid=(tr[o].l+tr[o].r)>>1;
if(L<=mid)ans=max(ans,query(2*o,L,R));
if(R>mid)ans=max(ans,query(2*o+1,L,R));
return ans;
}
void update(int o,int pos,int num)
{
if(tr[o].l==tr[o].r&&tr[o].l==pos){tr[o].mx=num;return ;}
int mid=(tr[o].l+tr[o].r)>>1;
if(pos<=mid)update(2*o,pos,num);
else update(2*o+1,pos,num);
tr[o].mx=max(tr[2*o].mx,tr[2*o+1].mx);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d%d",&po[i].x,&po[i].w),ve.push_back(po[i].x+po[i].w),dp[i]=1;
sort(po+1,po+n+1,cmp);
sort(ve.begin(),ve.end());
build(1,1,n);
for(int i=1;i<=n;i++)
{
int tep=po[i].x-po[i].w;
int pos=upper_bound(ve.begin(),ve.end(),tep)-ve.begin();
int p=lower_bound(ve.begin(),ve.end(),po[i].x+po[i].w)-ve.begin()+1;
if(pos>0)dp[p]=max(dp[p],query(1,1,pos)+1);
update(1,p,dp[p]);
}
printf("%d\n",query(1,1,n));
return 0;
}
B. Clique Problem(贪心)的更多相关文章
- CF #296 (Div. 1) B. Clique Problem 贪心(构造)
B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #296 (Div. 1) B. Clique Problem 贪心
B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #296 (Div. 2) D. Clique Problem [ 贪心 ]
传送门 D. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- [CF527D] Clique Problem - 贪心
数轴上有n 个点,第i 个点的坐标为xi,权值为wi.两个点i,j之间存在一条边当且仅当 abs(xi-xj)>=wi+wj. 你需要求出这张图的最大团的点数. Solution 把每个点看作以 ...
- CodeForces - 527D Clique Problem (图,贪心)
Description The clique problem is one of the most well-known NP-complete problems. Under some simpli ...
- [codeforces 528]B. Clique Problem
[codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...
- Codeforces Round #296 (Div. 1) B - Clique Problem
B - Clique Problem 题目大意:给你坐标轴上n个点,每个点的权值为wi,两个点之间有边当且仅当 |xi - xj| >= wi + wj, 问你两两之间都有边的最大点集的大小. ...
- 回溯法——最大团问题(Maximum Clique Problem, MCP)
概述: 最大团问题(Maximum Clique Problem, MCP)是图论中一个经典的组合优化问题,也是一类NP完全问题.最大团问题又称为最大独立集问题(Maximum Independent ...
- codeforces 442B B. Andrey and Problem(贪心)
题目链接: B. Andrey and Problem time limit per test 2 seconds memory limit per test 256 megabytes input ...
随机推荐
- DP专题·三(01背包+完全背包)
1.hdu 2126 Buy the souvenirs 题意:给出若干个纪念品的价格,求在能购买的纪念品的数目最大的情况下的购买方案. 思路:01背包+记录方案. #include<iostr ...
- Visual Studio 2012的Windows Service服务安装方式
windows服务应用程序是一种长期运行在操作系统后台的程序,它对于服务器环境特别适合,它没有用户界面,不会产生任何可视输出,任何用户输出都回被写进windows事件日志.计算机启动时,服务会自动开始 ...
- 12 Spring框架 SpringDAO的事务管理
上一节我们说过Spring对DAO的两个支持分为两个知识点,一个是jdbc模板,另一个是事务管理. 事务是数据库中的概念,但是在一般情况下我们需要将事务提到业务层次,这样能够使得业务具有事务的特性,来 ...
- Loadrunder场景设计篇——定时器(schedule)
A. 定义方案schedule 在 Scenario Schedule面板中,选择一个方案schedule,或通过点击New Schedule定义一个新的方案 定义schedule: a.新建sc ...
- Leaflet API 翻译(二)
摘自:http://www.ithao123.cn/content-824673.html L.Point 显示以像素为单位的点的x,y坐标. 所以接受点对象的leaflet方法和选项都也接受他们简单 ...
- 【leetcode刷题笔记】Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- js算法-快速排序(Quicksort)
快速排序(英语:Quicksort),又称划分交换排序(partition-exchange sort),简称快排,一种排序算法,最早由东尼·霍尔提出.在平均状况下,排序n个项目要O(nLogn)次比 ...
- NOIP 合唱队形
描述 N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2…,K,他们的身高分别为T1,T2,…, ...
- echarts3结合openlayers2实现Map类型图表
网上查阅了部分资料,有些是用echarts2实现的,因echarts2无法满足项目中其他部分的要求,故只能采用echarts3(2017/9/18,echarts3官网突然把基于geo的demo下架了 ...
- hdu 5920 Wool 思路
Wool Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Problem D ...