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;
}
相关推荐
彷徨而立5 分钟前
【C++】 using声明 与 using指示
开发语言·c++
一只鲲28 分钟前
48 C++ STL模板库17-容器9-关联容器-映射(map)多重映射(multimap)
开发语言·c++
学行库小秘41 分钟前
基于门控循环单元的数据回归预测 GRU
人工智能·深度学习·神经网络·算法·回归·gru
_meow_1 小时前
数学建模 15 逻辑回归与随机森林
算法·数学建模·逻辑回归
1白天的黑夜11 小时前
链表-24.两两交换链表中的结点-力扣(LeetCode)
数据结构·leetcode·链表
智践行1 小时前
C++11 智能指针:`std::unique_ptr`、`std::shared_ptr`和`std::weak_ptr`
c++
智践行1 小时前
C++11之后的 Lambda 表达式 以及 `std::function`和`std::bind`
c++
智践行2 小时前
C++11移动语义‘偷梁换柱’实战
c++
二向箔reverse2 小时前
机器学习算法核心总结
人工智能·算法·机器学习
祁同伟.2 小时前
【C++】模版(初阶)
c++