Codeforces 527D Clique Problem
2015-11-23 21:49
127 查看
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.
解题思路:这题看着吓人,求解200000W个点的无向图的最大团,然后就往怎么优化建图,怎么求解想去了。我不是傻吗?这是不可能的,后来发现原来这是一道思维题。对于xi,xj,如果xi>xj,则为xi-wi >= xj+wj;如果xi小于xj,则为xi+wi<=xj-wj,可以看出只要某一个点和xi在xi-wi,xi+wi区间内有交点则这两个点之间是不连边的,因此我们很容易看出这是一道求解最多不相交区间的题目,贪心算法解决即可。个人感觉CF上的思维题还是很赞的。。。
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.
解题思路:这题看着吓人,求解200000W个点的无向图的最大团,然后就往怎么优化建图,怎么求解想去了。我不是傻吗?这是不可能的,后来发现原来这是一道思维题。对于xi,xj,如果xi>xj,则为xi-wi >= xj+wj;如果xi小于xj,则为xi+wi<=xj-wj,可以看出只要某一个点和xi在xi-wi,xi+wi区间内有交点则这两个点之间是不连边的,因此我们很容易看出这是一道求解最多不相交区间的题目,贪心算法解决即可。个人感觉CF上的思维题还是很赞的。。。
#include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <string> #include <vector> #include <queue> #include <stack> #include <map> #include <set> #include <algorithm> #include <functional> using namespace std; struct Seg { int l, r; Seg() { } Seg(int _l, int _r) : l(_l), r(_r) { } bool operator < (const Seg &s) const { return r < s.r; } }; vector<Seg> svec; int main() { //freopen("aa.in", "r", stdin); int n, x, w; scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%d %d", &x, &w); svec.push_back(Seg(x-w, x+w)); } sort(svec.begin(), svec.end()); int ans = 1, curp = svec[0].r; for(int i = 1; i < n; ++i) { if(svec[i].l < curp) continue; else { ans++; curp = svec[i].r; } } printf("%d\n", ans); return 0; }
相关文章推荐
- Modelsim/QuestaSim教程——DO文件篇
- UIViewController
- UIBarButtonItem、UINavigationController
- leetcode 128:Longest Consecutive Sequence
- 兔子-build.gradle中代码的含义
- 【Android UI设计与开发】8.顶部标题栏(一)ActionBar 奥义·详解
- 一种导致UITextView输入中文却先输入拼音的解决思路
- HDOJ 5567 sequence1 (暴力)
- UIAlertController与 UIAlertView
- GUI对话框
- iOS开发--UITableViewCell侧滑多个按钮(系统仅支持iOS8以上)
- Handler Looper MessageQueue的应用
- UITableviewcell的删除操作
- iOS 中的线程(1)---①performSelectorInBackground②NSThread③NSOperation 和 NSOperationQueue 的组合
- MySQL query / clause execution order
- ueditor过滤
- IOS之UI -- UITableView -- 1 -- 相关初识
- [android] 优化UI卡顿现象
- xmanager - xstart - The X11 forwarding request was rejected
- Kibana User Guide [4.2] » Using Kibana in a Production Environment