Codeforces Round #296 (Div. 2) D. 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
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
题意:
选出一个最大的集合,集合里面的点两两之间满足:|xi - xj| ≥ wi + wj.
题解:
贪心,先按x排序。设xj<xi<xk
若xi-xj>=wi+wj;
xk-xi>=wk+wi;
则xk-xj>=wk+wj+2*wi>=wk+wj。
故相邻的点之间满足条件,则所有点均满足条件。
继续,xi-xj>=wi+wj; 等价于 xi-wi>=xj+wj;
即前一个点的x+w的和要尽可能小。
1.若当前点与前一个点满足条件,则ans++,xnow=x[i],wnow=w[i];
2.若与前一个点不满足条件,则看 x+w的和的关系,若x+w<xnow+wnow,由于x是递增关系,x-w必然更大,则与前一个点满足条件的集合,肯定也与当前点满足。
故舍弃前一个点,取当前点xnow=x[i],wnow=w[i]; 反之,舍弃当前点。
10349119 | 2015-03-19 16:14:45 | njczy2010 | D - Clique Problem | GNU C++ | Accepted | 93 ms | 1384 KB |
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <set> #define ll long long
int const N = ;
int const M = ;
int const inf = ;
ll const mod = ; using namespace std; int n;
int xnow,wnow;
int ans; typedef struct
{
int x;
int w;
}PP; PP p[N]; bool cmp(PP a,PP b)
{
return a.x<b.x;
} void ini()
{
ans=;
int i;
for(i=;i<=n;i++){
scanf("%d%d",&p[i].x,&p[i].w);
}
sort(p+,p++n,cmp);
} void solve()
{
int i;
ans=;
xnow=p[].x;
wnow=p[].w;
for(i=;i<=n;i++){
if(p[i].x-xnow>=p[i].w+wnow){
xnow=p[i].x;
wnow=p[i].w;
ans++;
}
else{
if(p[i].x+p[i].w<xnow+wnow){
xnow=p[i].x;
wnow=p[i].w;
}
}
}
} void out()
{
printf("%d\n",ans);
} int main()
{
// freopen("data.in","r",stdin);
//scanf("%d",&T);
//for(cnt=1;cnt<=T;cnt++)
while(scanf("%d",&n)!=EOF)
{
ini();
solve();
out();
}
}
Codeforces Round #296 (Div. 2) D. Clique Problem [ 贪心 ]的更多相关文章
- 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. 1) B - Clique Problem
B - Clique Problem 题目大意:给你坐标轴上n个点,每个点的权值为wi,两个点之间有边当且仅当 |xi - xj| >= wi + wj, 问你两两之间都有边的最大点集的大小. ...
- 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) C. Data Center Drama 欧拉回路
Codeforces Round #296 (Div. 1)C. Data Center Drama Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xx ...
- Codeforces Round #367 (Div. 2) C. Hard problem
题目链接:Codeforces Round #367 (Div. 2) C. Hard problem 题意: 给你一些字符串,字符串可以倒置,如果要倒置,就会消耗vi的能量,问你花最少的能量将这些字 ...
- Codeforces Round #603 (Div. 2) A. Sweet Problem(水.......没做出来)+C题
Codeforces Round #603 (Div. 2) A. Sweet Problem A. Sweet Problem time limit per test 1 second memory ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #296 (Div. 1) E. Triangles 3000
http://codeforces.com/contest/528/problem/E 先来吐槽一下,一直没机会进div 1, 马力不如当年, 这场题目都不是非常难,div 2 四道题都是水题! 题目 ...
- Codeforces Round #361 (Div. 2) C.NP-Hard Problem
题目连接:http://codeforces.com/contest/688/problem/C 题意:给你一些边,问你能否构成一个二分图 题解:二分图:二分图又称作二部图,是图论中的一种特殊模型. ...
随机推荐
- Android小玩意儿-- 从头开发一个正经的MusicPlayer(一)
之前从未接触过音乐播放器这块东西的开发.今天偶然想做一个自己的音乐播放器.算是练练手.既然要做,就要做一个正儿八经的App.很多网上的资料也是模模糊糊,不是很全,现在开始,自己摸索着尝试着一步一步的做 ...
- 利用JSTL重写查询的jsp页面
利用JSTL重写Java Web MVC实例中的jsp页面 第一步:导入jstl.jar和standard.jar文件
- 一段字符串中间提取json字符串
项目过程中经常打日志:LOG.error("[failure][CreateOrder] param:{}", JSON.toJSONString(userCreateOrderD ...
- java将一个List赋值给另一个List的4种方法
编辑 删除 声明:ArrayList a, 仅仅只是声明了一个list变量,其未来作用相当于C++中的引用变量,亦或者相当于一个对象块的索引,但并未为其分配具体的完整的对象所需要的内存空间,其所分配的 ...
- 第十三周翻译:SQL Server安全级别1:SQL Server安全性概述
SQL Server安全级别1:SQL Server安全性概述 源自:Stairway to SQL Server Security Level 1: Overview of SQL Server S ...
- VBA 从sql存储过程-记录集-导入
cnn.Open cnnstr cmd.ActiveConnection = cnn cmd.CommandTimeout = 120 cmd.CommandText = "dbo.t_bi ...
- Django展示第一个网页
展示一个网页需要三部分组成: urls.py -- 指定网址与对应的视图 views.py -- 创建试图以及指定对应的模板 template/*.html -- 对应的模板 一.urls.py ur ...
- SQLite -创建数据库
SQLite -创建数据库 SQLite sqlite3命令用于创建新的SQLite数据库.你不需要有任何特权来创建一个数据库. 语法: sqlite3命令的基本语法如下: $sqlite3 Data ...
- Linux之常用Shell脚本总结
一.简介本文将总结一些常用的shell脚本,方便以后工作中使用. 二.shell脚本[a]定期备份mysql数据库,需结合cronb定时任务调度实现. #!/bin/bash#首先声明一些自定义变量 ...
- 小程序08 小程序访问服务器API
后台交互 小程序是前端框架,需要和后台交互,本次课程主要介绍网络API. 小程序提供的网络访问API wx.request接口 发起 HTTPS 网络请求. 使用rqeust接口前的工作 1.小程序需 ...