codeforces 528B Clique Problem (贪心 + 思维)

探讨了在特定条件下的最大团问题,即在由坐标轴上的点构成的图中寻找最大团。通过将问题转化为寻找数轴上最多不重叠的线段数量,使用贪心算法解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Input
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.

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

Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
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.

在这里插入图片描述

题目链接

http://codeforces.com/contest/528/problem/B

题目大意

在坐标轴上有N个点,每个点权重为w。若满足两点距离大于两点权重之和,则这两点可以连接起来。问已知的N个点中最多有多少点满足两两相连

数据范围

n (1 ≤ n ≤ 200 000)
xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109)

解题思路

已知|x1 - x2|>w1 + w2;
则当x1 > x2时,x1 - w1 > x2 + w2;
当x2 > x1时,x2 - w2 > x1 + w1;
此题可转化为将每点变为长度为2w的线段(x - w,x + w),判断数轴上最多能存在多少不重叠的线段,即为本题答案。此时用贪心,先x + w小的优先。
类似“今年暑假不AC”

解决代码

#include<cstdio>
#include<algorithm>
using namespace std;
struct A{
int l,r;
}a[200005];
bool cmp(A x,A y){
return x.r < y.r;
}
int main()
{
int n,x,w,ans = 0,t = -1000000005;
scanf("%d",&n);
for(int i = 1;i <= n;i++){
	scanf("%d %d",&x,&w);
	a[i].l = x - w;
	a[i].r = x + w;
}
sort(a + 1,a + n + 1,cmp);

for(int i = 1;i <= n;i++){
	if(a[i].l >= t) {
		t = a[i].r;
		ans++;
		}
	}
	printf("%d\n",ans);
return 0;
}
### 关于 Codeforces 上二项装箱问题 #### 二项装箱问题概述 二项装箱问题是经典的组合优化问题之一,在计算机科学领域具有重要意义。该类问题通常涉及将一组不同大小的对象放入固定容量的容器中,目标是最小化使用的容器数量[^1]。 对于特定平台上的挑战实例,如Codeforces中的二项装箱问题,其核心在于设计高效算法来解决这一NP难问题。尽管找到最优解可能非常复杂,但存在多种启发式方法可以提供接近最佳的结果,并且这些方法能够在合理的时间内执行完毕。 #### 解决方案策略 一种常见的处理方式是采用贪心算法,即总是尝试把当前最大的未分配物品放置到第一个能够容纳它的箱子中;如果没有任何现有箱子能放下这件物品,则创建一个新的箱子用于装载它。这种方法简单易懂,但在某些情况下可能会导致次优解。 更复杂的近似算法包括首次适应下降(First Fit Decreasing, FFD),此技术首先按照降序排列所有项目尺寸,之后应用首次适配原则(FD)。FFD已被证明能在多项式时间内给出不超过理想最小值11/9倍数加四的解法质量保证[^2]。 此外还有其他高级求解途径比如动态规划、分支限界以及遗传算法等,它们各自适用于不同的应用场景并提供了不同程度上的性能改进。 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; cin >> n; vector<int> items(n); for(int i = 0; i < n; ++i){ cin >> items[i]; } sort(items.begin(), items.end(), greater<int>()); const int bin_capacity = 1000; // 假设每个bin的最大容量为1000单位体积 vector<int> bins; for(auto item : items){ bool placed = false; for(auto& b : bins){ if(b + item <= bin_capacity){ b += item; placed = true; break; } } if(!placed){ bins.push_back(item); } } cout << "Minimum number of bins required is: " << bins.size(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值