1 条题解
-
1
#include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; struct Point { int x, y; }; int n; vector<Point> points; // 并查集实现 class UnionFind { vector<int> parent; public: UnionFind(int n) { parent.resize(n); for(int i=0; i<n; i++) parent[i] = i; } int find(int x) { if(parent[x] != x) parent[x] = find(parent[x]); return parent[x]; } void unite(int x, int y) { int fx = find(x), fy = find(y); if(fx != fy) parent[fx] = fy; } bool connected() { int root = find(0); for(int i=1; i<n; i++) { if(find(i) != root) return false; } return true; } }; // 检查在时间t时是否所有点都连通 bool isConnected(int t) { UnionFind uf(n); for(int i=0; i<n; i++) { for(int j=i+1; j<n; j++) { int dx = abs(points[i].x - points[j].x); int dy = abs(points[i].y - points[j].y); if(dx + dy <= 2 * t) { uf.unite(i, j); } } } return uf.connected(); } int main() { cin >> n; points.resize(n); for(int i=0; i<n; i++) { cin >> points[i].x >> points[i].y; } // 二分查找最小的时间t int left = 0, right = 0; // 先找到最大可能的t for(int i=0; i<n; i++) { for(int j=i+1; j<n; j++) { int d = abs(points[i].x - points[j].x) + abs(points[i].y - points[j].y); right = max(right, (d + 1) / 2); } } int ans = right; while(left <= right) { int mid = left + (right - left) / 2; if(isConnected(mid)) { ans = mid; right = mid - 1; } else { left = mid + 1; } } cout << ans << endl; return 0; }
- 1
信息
- ID
- 361
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- 9
- 标签
- 递交数
- 9
- 已通过
- 4
- 上传者