The 2024 ICPC Kunming Invitational Contest

VP链接:https://codeforces.com/gym/105386

B. Gold Medal

签到题。对每一个读入的数 a,先记录已有奖牌数量,即 ,再将 a 对 k 取模。然后将 a 数组从大到小排序,将每个不足 k 的数补到 k。如果 m 有剩余,

时间复杂度:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int N = 105;
int n, a[N], k, m;

signed main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int t; cin >> t;
	while (t--) {
		cin >> n >> k;
		int ans = 0;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
			ans += a[i] / k;
			a[i] %= k;
		}
		sort(a + 1, a + n + 1);
		cin >> m;
		for (int i = n; i; i--) {
			if (a[i] + m >= k) {
				ans++;
				m -= (k - a[i]);
			} else break;
		}
		ans += m / k;
		cout << ans << '\n';
	}
	return 0;
}

G. Be Positive

当 n = 1 和 n % 4 = 0 的时候,都没有合法方案,输出 impossible。

其余情况,首先初始化 。当 i % 4 = 0 的时候,将第 i 位的第 i - 1 位交换位置。还要将第 0 位和第 1 位交换位置。

时间复杂度:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 10;
int a[N];

inline int read() {
	int x = 0, f = 1; char c = getchar();
	while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}

void solve() {
	int n = read();
	if (n == 1 || n % 4 == 0) {
		puts("impossible");
		return;
	}
	for (int i = 2; i < 25; i++) {
		if (n == pw[i]) {
			puts("impossible");
			return;
		}
	}
	for (int i = 0; i < n; i++) a[i] = i;
	swap(a[0], a[1]);
	for (int i = 0; i < n; i++) {
		if (i && i % 4 == 0) swap(a[i - 1], a[i]);
	}
	for (int i = 0; i < n; i++) printf("%d ", a[i]);
	puts("");
}

signed main() {
	int t = read();
	while (t--) {
		solve();
	}
	return 0;
}

I. Left Shifting 2

当字符串首尾字符相同的时候,让首尾相同的字符全部放到末尾,方便处理。

可以注意到,

  • 当整个字符串所有字符都相同的时候,操作次数为字符串长度 / 2。
  • 当字符串所有连续相同字符长度都为奇数时,操作次数为连续相同字符长度 / 2 的和。
  • 当字符串所有连续相同字符长度存在偶数时,操作次数为连续相同字符长度 / 2 的和 - 1,因为通过左移操作可以让一个长度为偶数的连续相同字符放到字符串两端且各自为奇数,这样就减少了一次操作次数。
cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

signed main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int t; string s;
	cin >> t;
	while (t--) {
		cin >> s;
		if (s.size() && s[0] == s[s.size() - 1]) {
			int i;
			for (i = 0; i < s.size(); i++) {
				if (i + 1 < s.size() && s[i + 1] != s[0]) break;
			}
			int size = s.size() - 1;
			i = min(i, size);
			if (i + 1 < s.size()) s = s.substr(i + 1, s.size() - i - 1) + s.substr(0, i + 1);
		} // 处理首尾字符相等的字符串
		int ans = 0, flag = 0, res = 1;
		for (int i = 0; i < s.size(); i++) {
			if (i - 1 >= 0 && s[i] == s[i - 1]) res++;
			else {
				ans += res / 2;
				if (res % 2 == 0) flag = 1;
				res = 1;
			}
		}
		if (res % 2 == 0 && res != s.size()) flag = 1;
		ans += res / 2;
		if (flag) ans--;
		cout << ans << '\n';
	}
    return 0;
}

E. Relearn through Review

对于给定操作区间 ,最终答案由

这三部分的 gcd 组成。

如果 r 的值是确定的,那么最后一项就是确定的。第一项(前缀)的值只可能由 种,X 为 前缀种最小的 a。所以只需要分别枚举左右端点即可。

时间复杂度:,因为前缀只有 种,所以真正有意义的左端点只会有 个。

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;

#define int long long

inline int read() {
	int x = 0, f = 1; char c = getchar();
	while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}

int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); }

signed main() {
	int t = read();
	while (t--) {
		int n = read(), k = read();
		vector<int> pre(n + 1), suf(n + 2), a(n + 1);
		for (int i = 1; i <= n; i++) a[i] = read();
		for (int i = 1; i <= n; i++) pre[i] = gcd(pre[i - 1], a[i]);
		for (int i = n; i; i--) suf[i] = gcd(suf[i + 1], a[i]);
		int ans = pre[n];
		for (int l = 1; l <= n; l++) {
			if (pre[l] == pre[l - 1]) continue;
			int g = 0;
			for (int r = l; r <= n; r++) {
				g = gcd(g, a[r] + k);
				ans = max(ans, gcd(pre[l - 1], gcd(g, suf[r + 1])));
			}
		}
		printf("%lld\n", ans);
	}
	return 0;
}
相关推荐
RuoZoe5 小时前
重塑WPF辉煌?基于DirectX 12的现代.NET UI框架Jalium
c语言
blasit6 小时前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
AI软著研究员7 小时前
程序员必看:软著不是“面子工程”,是代码的“法律保险”
算法
FunnySaltyFish7 小时前
什么?Compose 把 GapBuffer 换成了 LinkBuffer?
算法·kotlin·android jetpack
颜酱8 小时前
理解二叉树最近公共祖先(LCA):从基础到变种解析
javascript·后端·算法
地平线开发者1 天前
SparseDrive 模型导出与性能优化实战
算法·自动驾驶
董董灿是个攻城狮1 天前
大模型连载2:初步认识 tokenizer 的过程
算法
地平线开发者1 天前
地平线 VP 接口工程实践(一):hbVPRoiResize 接口功能、使用约束与典型问题总结
算法·自动驾驶
罗西的思考1 天前
AI Agent框架探秘:拆解 OpenHands(10)--- Runtime
人工智能·算法·机器学习
HXhlx1 天前
CART决策树基本原理
算法·机器学习