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;
}
相关推荐
Frank_zhou4 小时前
算法-链表实战【删除链表的倒数第 N 个结点】中等
数据结构
王中阳Go5 小时前
从超市收银到航空调度:贪心算法如何破解生活中的最优决策谜题?
java·后端·算法
故事挺秃然6 小时前
中文分词:机械分词算法详解与实践总结
算法·nlp
知然8 小时前
鸿蒙 Native API 的封装库 h2lib_arkbinder
c++·arkts·鸿蒙
十五年专注C++开发8 小时前
Qt .pro配置gcc相关命令(三):-W1、-L、-rpath和-rpath-link
linux·运维·c++·qt·cmake·跨平台编译
车队老哥记录生活8 小时前
【MPC】模型预测控制笔记 (3):无约束输出反馈MPC
笔记·算法
Cai junhao8 小时前
【Qt】Qt控件
开发语言·c++·笔记·qt
uyeonashi9 小时前
【QT系统相关】QT网络
开发语言·网络·c++·qt
地平线开发者9 小时前
BEV 感知算法评价指标简介
算法·自动驾驶
不过四级不改名6779 小时前
用c语言实现简易c语言扫雷游戏
c语言·算法·游戏