跳到正文

个人记录

LZM_一期生成树测试

King_tissue约 6 分钟
原文
文章信息与版本

文章信息

收录
2026年7月24日
更新
2026年7月24日
篇幅
约 6 分钟

版本

  1. v1当前

我要阐述我的七宗罪。

40 + 100 + 0 = 140, rk 10

T1 P17016

第一罪:傲慢之罪,不检查数组大小。

第二罪:贪婪之罪,喜欢贪速度不检查。

第三罪:等我想一想。

数组开小了少了 60 pts

code
C++
#include <bits/stdc++.h>
using namespace std;
int x[5005], y[5005], fu[5005];
struct bian {
	int u, v; double w;
} b[1000005];
bool cmp (bian qwe, bian ewq) {
	return qwe.w < ewq.w;
}
int find (int x) {
	if (fu[x] == x) return x;
	return fu[x] = find(fu[x]);
} 
int main() {
	int n; double l;
	cin >> n >> l;
	for (int i = 1; i <= n; i++) {
		cin >> x[i] >> y[i]; fu[i] = i;
	}
	int tot = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = i + 1; j <= n;j ++) {
			b[++tot].w = sqrt(1.0 * (x[i] - x[j]) * (x[i] - x[j]) + 1.0 * (y[i] - y[j]) * (y[i] - y[j]));
			b[tot].u = i, b[tot].v = j;
		} 
	}
	sort (b + 1, b + tot + 1, cmp);
	double ans = 0.0;
	for (int i = 1; i <= tot; i++) {
		int u = b[i].u, v = b[i].v; double w = b[i].w;
		if (w > l) break;
//		cout << u << " " << v << " " << w << "\n";
		if (fu[find(u)] == fu[find(v)]) continue;
		if (find(v) < find(u)) swap(u, v);
		fu[find(v)] = fu[find(u)];
		ans += w;
	}
	for (int i = 1; i <=n; i++) {
//		cout << fu[find(i)] << " ";
		if (fu[find(i)] != 1) {
			cout << "Impossible" << "\n";
			return 0; 
		}
	}
//	cout << ans << "\n";
	printf("%.2lf\n", ans);
	return 0;
}

T2 P13736

也是垃圾题目。

直接考虑往后面的每一列都找比它高的和比它矮的。然后直接施法每个点连两条边,以及每一列里面的互相连一下。于是再跑一次最小生成树就做完了。

但是别看这个思路看起来无比有道理,但是我一直错样例所以果断废弃了这个何意味想法。

观察题目。设天数为 x,x 增大连接的岛肯定不会变少,于是考虑二分。

二分出一个天数 x,就相当于每个岛向下延伸 x 格。

存每一列的岛的 y 坐标和编号,枚举每个点 i

因为每个点只能连接此列和左右列的点(其实还是原来的想法),找到这三列最后一个可以连接的点 p,使用并查集将 i 和 p 合并。

时间复杂度大约是 O(n log2 h)O(n \ log^2 \ h),代码像粑粑。

code
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5, INF = LONG_LONG_MAX;
int h, w, n, r[N], c[N], f[N];
int find(int x) {
	if (x == f[x])return x;
	return f[x] = find(f[x]);
}
void merge(int x, int y) {
	x = find(x), y = find(y);
	f[x] = y;
}
vector<pair<int,int> >s[N];
bool check(int x) {
	for (int i = 1; i <= n; i++)f[i] = i;
	for (int i = 1; i <= n; i++) {
		auto a = upper_bound(s[c[i] - 1].begin(), s[c[i] - 1].end(), make_pair(r[i] + x, INF));
		auto b = upper_bound(s[c[i]].begin(), s[c[i]].end(), make_pair(r[i] + x + 1, INF));
		auto d = upper_bound(s[c[i] + 1].begin(), s[c[i] + 1].end(), make_pair(r[i] + x, INF));
		if (a > lower_bound(s[c[i] - 1].begin(), s[c[i] - 1].end(), make_pair(r[i], 0ll))) {
			merge(i, (*--a).second);
		}
		if (b > lower_bound(s[c[i]].begin(), s[c[i]].end(), make_pair(r[i], 0ll))) {
			merge(i, (*--b).second);
		}
		if (d > lower_bound(s[c[i] + 1].begin(), s[c[i] + 1].end(), make_pair(r[i], 0ll))) {
			merge(i, (*--d).second);
		}
	}
	for (int i = 1; i <= n; i++) {
		if (find(i) != find(1)) return 0;
	}
	return 1;
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> h >> w >> n;
	for (int i = 1; i <= n; i++) {
		cin >> r[i] >> c[i];
		s[c[i]].push_back({r[i], i});
	}
	for (int i = 1; i <= w; i++) {
		sort(s[i].begin(), s[i].end());
	}
	int l = 0, r = h;
	while (l <= r) {
		int mid = l + r >> 1;
		if (check(mid)) {
			r = mid - 1;
		} else {
			l = mid + 1;
		}
	}
	if (r == h) cout << -1;
	else cout << r + 1;
	return 0;
}
原文