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;
}
相关推荐
寂静山林3 分钟前
UVa 1471 Defense Lines
算法
纵有疾風起22 分钟前
C++模版:模板初阶及STL简介
开发语言·c++·经验分享·开源
小莞尔24 分钟前
【51单片机】【protues仿真】基于51单片机送带计数器系统
c语言·单片机·嵌入式硬件·物联网·51单片机
QT 小鲜肉27 分钟前
【个人成长笔记】Qt Creator快捷键终极指南:从入门到精通
开发语言·c++·笔记·qt·学习·学习方法
CHANG_THE_WORLD39 分钟前
switch语句在汇编层面的几种优化方式 ,为什么能进行优化
汇编·算法·switch·汇编分析·switch case·switch case 汇编·switch case 语句
勇闯逆流河39 分钟前
【C++】用红黑树封装map与set
java·开发语言·数据结构·c++
实心儿儿39 分钟前
C++——内存管理
c++
山,离天三尺三39 分钟前
深度拷贝详解
开发语言·c++·算法
Blossom.1181 小时前
把AI“撒”进农田:基于极值量化与状态机的1KB边缘灌溉决策树
人工智能·python·深度学习·算法·目标检测·决策树·机器学习
一只鱼^_1 小时前
第 167 场双周赛 / 第 471 场周赛
数据结构·b树·算法·leetcode·深度优先·近邻算法·迭代加深