Codeforces 527D Clique Problem

本文针对 CodeForces 平台上的 527D 问题提供了一种有效的贪心算法解决方案。通过对点集进行特定排序并采用贪心策略,该算法能够找到满足条件的最大点集合。

http://codeforces.com/problemset/problem/527/D

 

题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集合中的点两两之间存在边

化开有

xi-xj>=wi+wj ==> wj+xj<=wi-xi

xj-xi>=wi+wj ==> wi+wx<=wj-xj

发现本质相同,我们按x+w排序,从最小的往大的贪心连边,因为如果有一条边,那么比它小的也全部可以连到边。

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<iostream>
 6 struct node{
 7     int x,w;
 8 }p[500005];
 9 int n;
10 int read(){
11     int t=0,f=1;char ch=getchar();
12     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
13     while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();}
14     return t*f;
15 }
16 bool cmp(node a,node b){
17     return a.w+a.x<b.x+b.w;
18 }
19 int main(){
20     n=read();
21     for (int i=1;i<=n;i++)
22      p[i].x=read(),p[i].w=read();
23     std::sort(p+1,p+1+n,cmp);
24     int ans=1,t=p[1].w+p[1].x;
25     for (int i=2;i<=n;i++){
26         if (t<=p[i].x-p[i].w){
27             ans++;
28             t=p[i].x+p[i].w;
29         }
30     } 
31     printf("%d\n",ans);
32     return 0;
33 }

 

转载于:https://www.cnblogs.com/qzqzgfy/p/5627186.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值