跳到正文

题解

Never庆生赛 题解

xuan_never约 13 分钟
原文
文章信息与版本

文章信息

发布
2024年5月27日
收录
2025年12月12日
更新
2026年11月6日
篇幅
约 13 分钟

版本

  1. v2当前
  2. v1

NeverOI Round 1&Never庆生赛

A / 签到题1

以 O(n)\mathcal{O}(n) 的时间复杂度可通过本题。 以类似动态规划的算法扫过。

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, a[6000006], l = 1, anl = 1, anr = 1;
ll ans, b;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n >> a[1];
	ans = b = a[1];
	for (int i = 2; i <= n; ++i) {
		cin >> a[i];
		if (b > 0)
			b += a[i];
		else b = a[i], l = i;
		if (b > ans)
			ans = b, anl = l, anr = i;
		else if (b == ans && i - l < anr - anl)
			anl = l, anr = i;
	} cout << ans << ' ' << anl << ' ' << anr;
	return 0;
}

B / 未曾设想的道路

不难发现,只要有边可以诱导算法走向错误的路即可,可 O(1)\mathcal{O}(1) 通过本题。

C++
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
	scanf("%d", &n);
	if (n <= 2) {
		printf("-1");
		return 0;
	} printf("%d 3\n1 2 0\n2 %d 9\n1 %d 1", n, n, n);
	return 0;
}

C / Star Shine

看见求和,想到前缀和。 维护两个斜向的前缀和,实现 O(1)\mathcal{O}(1) 判断,枚举点与 kk,最终复杂度 O(n2m+nm2)\mathcal{O}(n^2m+nm^2)。

C++
#include <bits/stdc++.h>
using namespace std;
int n, m, a[402][402], sum1[802][402], sum2[802][402], s, ans, ank = 1e9;
bool cnt, yes;
int gs1(int x1, int x2, int l) {
	if (l < 1 || l > n + m - 1) return 0;
	x1 = max((l > m ? l - m + 1 : 1), x1);
	x2 = min(min(n, l), x2);
	if (x1 > x2) return 0;
	x1 = min(min(n, l), x1);
	cnt = 1;
	return sum1[l][x2] - sum1[l][x1 - 1];
}
int gs2(int x1, int x2, int l) {
	if (l < 1 || l > n + m - 1) return 0;
	x1 = max((l > m ? l - m + 1 : 1), x1);
	x2 = min(min(n, l), x2);
	if (x1 > x2) return 0;
	x1 = min(min(n, l), x1);
	cnt = 1;
	return sum2[l][x2] - sum2[l][x1 - 1];
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
	cin >> n >> m;
	for (int i = 1; i <= n; ++i)
		for (int j = 1; j <= m; ++j)
			cin >> a[i][j],
			sum1[i - j + m][i] = sum1[i - j + m][i - 1] + a[i][j],
			sum2[i + j - 1][i] = sum2[i + j - 1][i - 1] + a[i][j];
	for (int x = 1, k; x <= n; ++x) {
		for (int y = 1; y <= m; ++y) {
			yes = 0;
			for (k = 1; k <= n + m; ++k) {
				cnt = 0;
				s = gs1(x, x + k, x - y + k + m) + gs1(x - k, x, x - y - k + m) + gs2(x - k + 1, x - 1, x + y - k - 1) + gs2(x + 1, x + k - 1, x + y + k - 1);
				if (!cnt) break;
				if (s == a[x][y]) {
					++ans, ank = min(k, ank);
					yes = 1;
					break;
				}
			}
		}
	} if (ans) cout << ans << ' ' << ank;
	else cout << -1;
	return 0;
}

D / 关系网

使用临时哈希或 unordered_map 都无法避免字符串带来的巨大常数,通过离散化(用自然数编号代替字符串)解决这点。 加上最小生成树,最终复杂度 O(nlog⁡n+mlog⁡m)\mathcal{O}(n\log n+m\log m)。

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, pair<int, int>> pip;
#define fi first
#define se second
int n, m, fa[20004], cnt, u, v, w;
ll ans;
string s1, s2;
map<string, int> a;
int find(int u) {
	return fa[u] == u ? u : fa[u] = find(fa[u]);
}
vector<pip> edge;
int add(string s) {
	if (!a[s])
		a[s] = ++cnt;
	return a[s];
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
	cin >> n >> m;
	for (int i = 0; i < m; ++i) {
		cin >> s1 >> s2 >> w;
		u = add(s1), v = add(s2);
		if (w) edge.emplace_back(w, make_pair(u, v));
	} if (edge.size() < n - 1 || a.size() < n) {
		cout << "sto";
		return 0;
	} sort(edge.begin(), edge.end(), greater<pip>());
	iota(fa + 1, fa + 1 + n, cnt = 1);
	for (pip i : edge) {
		u = find(i.se.fi), v = find(i.se.se);
		if (u != v) {
			fa[u] = v, ans += i.fi;
			if (++cnt == n) break;
		}
	} if (cnt >= n) cout << ans;
	else cout << "sto";
	return 0;
}

E / 杂交水题

可线性预处理 A,BA,B,递归次数通过记忆化搜索的方式预处理,复杂度 O(n)\mathcal{O}(n),O(1)\mathcal{O}(1) 询问。

C++
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#define fi first
#define se second
const int maxn = 5200000, M = maxn << 1;
int c[maxn], a[maxn], b[maxn], pri[maxn >> 1], ps, A[maxn], B[M], F[maxn], T, n;
bool np[maxn];
queue<pii> lis;
void init1() {
	a[1] = b[1] = 1;
	for (int i = 2, j; i < maxn; ++i) {
		if (!np[i]) {
			pri[ps++] = i;
			c[i] = 1, a[i] = 2;
			b[i] = i - 1;
		} for (int k = 0; k < ps; ++k) {
			j = pri[k];
			if (i * j >= maxn) break;
			np[i * j] = 1;
			if (i % j == 0) {
				c[i * j] = c[i] + 1,
				a[i * j] = a[i] / c[i * j] * (c[i * j] + 1);
				b[i * j] = b[i] * j;
				break;
			} else {
				c[i * j] = 1,
				a[i * j] = 2 * a[i];
				b[i * j] = b[i] * b[j];
			}
		} A[i] = i - a[i] - b[i] + 1;
	}
}
void init2() {
	lis.push({2, B[1] = B[2] = 1});
	while (!lis.empty()) {
		int x = lis.front().fi, t = lis.front().se, y = x << 1;
		lis.pop(), ++t;
		if (y < M && !B[y])
			lis.push({y, B[y] = t});
		y = x + 1;
		if (y < M && !B[y])
			lis.push({y, B[y] = t});
		y = x - 1;
		if (!B[y])
			lis.push({y, B[y] = t});
	} B[1] = 0;
}
int f(int x) {
	if (!x) return 1;
	if (~F[x]) return F[x];
	if (x & 1) return F[x] = f(B[x]) + 1;
	else return F[x] = f(A[x]) + 1;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
	init1();
	init2();
	memset(F, -1, sizeof F);
	for (int i = 1; i < maxn; ++i)
		f(i);
	for (cin >> T >> n; T--; ) {
		n = (n - 1 + 191) % 5141919 + 1;
		cout << F[n] << '\n';
	}
	return 0;
}

F / 记分

采用动态规划,以最后一次的记录为状态,但是数值很大,无法开下数组。 注意到能记录出来的数字是有限的,个数不会超过 2n2n,可以使用 map 代替数组。 记 aia_i 的最大位数为 ss,则总时间复杂度为 O(n2s2log⁡n)\mathcal{O}(n^2s^2\log n),实际常数非常小,可以通过此题。

C++
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#define fi first
#define se second
int n, a[2222], ans[2222];
int sz(int x) {
	int an = 1;
	do ++an, x /= 10; while (x);
	return an;
}
int f[11][11];
vector<int> A, B;
int gg(int x, int y) {
	bool an = (x >= 0 && y < 0);
	A = {0};
	if (x < 0) x *= -1;
	do A.insert(A.begin() + 1, x % 10),
		x /= 10;
	while (x);
	B = {0};
	if (y < 0) y *= -1;
	do B.insert(B.begin() + 1, y % 10),
		y /= 10;
	while (y);
	for (int i = 1; i < A.size(); ++i)
		for (int j = 1; j < B.size(); ++j) {
			f[i][j] = max(f[i - 1][j], f[i][j - 1]);
			if (A[i] == B[j])
				f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1);
		}
	return an + B.size() - 1 - f[A.size() - 1][B.size() - 1];
}
map<int, int> dp[2222];
void DP() {
	dp[2][a[2]] = ans[1] + sz(a[2]),
	dp[2][a[1] + a[2]] = sz(a[1]) + gg(a[1], a[1] + a[2]);
	ans[2] = min(dp[2][a[2]], dp[2][a[1] + a[2]]);
	for (int i = 3; i <= n; ++i) {
		ans[i] = min(ans[i], dp[i][a[i]] = ans[i - 1] + sz(a[i]));
		for (pii j : dp[i - 1])
			ans[i] = min(ans[i], dp[i][j.fi + a[i]] = j.se + gg(j.fi, j.fi + a[i]));
	}
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
	cin >> n;
	for (int i = 1; i <= n; ++i)
		cin >> a[i], ans[i] = 2e9;
	ans[1] = sz(a[1]) - (a[1] >= 0);
	if (n < 2) {
		cout << ans[1];
		return 0;
	} DP();
	for (int i = 1; i <= n; ++i)
		cout << ans[i] << ' ';
	return 0;
}
原文