Codeforces Round #296 (Div. 1) B - Clique Problem

B - Clique Problem

题目大意:给你坐标轴上n个点,每个点的权值为wi,两个点之间有边当且仅当 |xi - xj| >= wi + wj, 问你两两之间都有边的最大点集的大小。

思路:其实问题能转换为一堆线段问你最多能挑出多少个线段使其两两不相交。。 对于一个点来说可以变成[x - wi, x + wi]这样一条线段。。

然后贪心取就好啦。

 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 #define fi first
 4 #define se second
 5 #define mk make_pair
 6 #define pii pair<int,int>
 7 #define piii pair<int, pair<int,int>>
 8 
 9 using namespace std;
10 
11 const int N=2e5+7;
12 const int M=1e4+7;
13 const int inf=0x3f3f3f3f;
14 const LL INF=0x3f3f3f3f3f3f3f3f;
15 const int mod=1e9 + 7;
16 
17 int n;
18 pii p[N];
19 int main() {
20     scanf("%d", &n);
21     for(int i = 1; i <= n; i++) {
22         int x, w; scanf("%d%d", &x, &w);
23         p[i].first = x - w;
24         p[i].second = x + w;
25     }
26     sort(p + 1, p + 1 + n);
27     stack<pii> st;
28     for(int i = 1; i <= n; i++) {
29         if(st.empty()) st.push(p[i]);
30         else if(p[i].first >= st.top().second) st.push(p[i]);
31         else if(p[i].second < st.top().second) st.pop(), st.push(p[i]);
32     }
33     printf("%d\n", st.size());
34     return 0;
35 }
36 /*
37 */

猜你喜欢

转载自www.cnblogs.com/CJLHY/p/9036750.html