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;
}
相关推荐
小π军12 分钟前
STL利器:upper_bound与lower_bound的使用
c++
在下赵某人14 分钟前
概率数据结构的设计原理与误差分析
数据结构·算法·哈希算法
fashion 道格17 分钟前
深入理解数据结构中的图:邻接链表的应用与实现
数据结构·链表
CoderYanger22 分钟前
递归、搜索与回溯-综合练习:19.目标和
java·算法·leetcode·1024程序员节
mit6.82422 分钟前
dfs|mask^翻转
算法
SKYDROID云卓小助手40 分钟前
三轴云台之控制协同技术
服务器·网络·图像处理·人工智能·算法
Zx623651 小时前
13.泛型编程 STL技术
java·开发语言·c++
The Last.H1 小时前
Educational Codeforces Round 185 (Rated for Div. 2)A-C
c语言·c++·算法
caron41 小时前
C++ 推箱子游戏
开发语言·c++·游戏
客梦1 小时前
数据结构基本知识
数据结构