【CodeForces】Educational Codeforces Round 47 题解

本文详细解析了Codeforces的Educational Codeforces Round 47比赛中的七道题目,包括A到G题。每个题目都提供了思路要点,时间复杂度分析以及相应的代码实现。题目涉及模拟、字符串操作、最优化问题、图论和线段树等算法知识。
摘要由CSDN通过智能技术生成

【比赛链接】

【题解链接】

**【A】**Game Shopping

【思路要点】

  • 按照题意模拟。
  • 时间复杂度 $ O(N) $ 。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
template <typename T> void chkmax(T &x, T y) {
    x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {
    x = min(x, y); } 
template <typename T> void read(T &x) {
    
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
    
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
    
	write(x);
	puts("");
}
int a[MAXN], b[MAXN];
int main() {
    
	int n, m; read(n), read(m);
	for (int i = 1; i <= n; i++)
		read(a[i]);
	for (int i = 1; i <= m; i++)
		read(b[i]);
	int ans = 0, pos = 1;
	for (int i = 1; i <= n && pos <= m; i++)
		if (b[pos] >= a[i]) {
    
			pos++;
			ans++;
		}
	writeln(ans);
	return 0;
}

**【B】**Minimum Ternary String

【思路要点】

  • 题目中 $ 0 $ 和 $ 2 $ 的相对位置始终不会发生改变,可以看做 $ 1 $ 可以自由移动, $ 0 $ 和 $ 2 $ 留在原地。
  • 将所有 $ 1 $ 插入到第一个 2 2 2 之前即可,若没有出现 2 2 2 ,则插到字符串末尾。
  • 时间复杂度 $ O(N) $ 。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
template <typename T> void chkmax(T &x, T y) {
    x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {
    x = min(x, y); } 
template <typename T> void read(T &x) {
    
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
    
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
    
	write(x);
	puts("");
}
char s[MAXN];
int main() {
    
	scanf("%s", s + 1);
	int n = strlen(s + 1), cnt = 0;
	for (int i = 1; i <= n; i++)
		if (s[i] == '1') cnt++;
	for (int i = 1; i <= n; i++)
		if (s[i] != '1') {
    
			if (s[i] == '0') putchar(s[i]);
			else {
    
				if (cnt > 0) while (cnt--) putchar('1');
				putchar(s[i]);
			}
		}
	if (cnt > 0) while (cnt--) putchar('1');
	return 0;
}

**【C】**Annoying Present

【思路要点】

  • 使平均数最大即为使总和最大,若 d i ≥ 0 d_i≥0 di0 ,则选择在 1 1 1 号位置操作,否则在中间位置操作即可。
  • 中间量可能很大,需要 l o n g   d o u b l e long\ double long double
  • 时间复杂度 $ O(M) $ 。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
template <typename T> void chkmax(T &x
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值