Codeforces Round 884 (Div. 1 + Div. 2) A~D

Codeforces Round 884 (Div. 1 + Div. 2)

比赛链接:Codeforces Round 884 (Div. 1 + Div. 2)

A. Subtraction Game

题目链接:Problem - A - Codeforces

题意:n个石头,两个人拿,每次可以拿a个或者b个,问多少个石头是后手必胜。

题解:直接输入a+b即可

//
// Created by lv_shen on 2023/7/11.
//

#include <bits/stdc++.h>

#define endl "\n"
using namespace std;
typedef long long LL;
typedef unsigned long long u64;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;

void solve ()
{
	int a, b;
	cin >> a >> b;
	cout << a + b  << endl;
}

int main ()
{
	std::ios::sync_with_stdio (false);
	std::cin.tie (nullptr);
	std::cout.tie (nullptr);

	int t;
	cin >> t;
	while (t--) {
		solve ();
	}

	return 0;
}

B. Permutations & Primes

原题链接:Problem - B - Codeforces

题意:给定一个n,要求输入一个n的全排列。这个全排列要求要满足mex(a[l]...a[r])为质数的(l,r)数对的数量最多

题解:n3时,将2和3放到两边,1放中间即可。下面给出一个非常不严谨且感性的证明。

因为要让mex是一个质数,必须要让这个区间内的数包含1,因此考虑将1放到中间,这样包含1的区间会更多(为什么中间是最多的呢?假设放在位置i,那么包含他的区间应该是i×(ni+1)=i2+(n+1)i个,当i=n2时最大),也就是不包含1的区间最少。同时要让这些包含1的区间不包含2,因此考虑将2放到边上,另外让同时包含1,2的区间尽量不包含3,因此将3放到另一边。这样的话所有区间(除了[1n])都满足要么不包含1,要么就包含1但不同时包含23。然而我们刚刚说这种情况下(把1放中间)不包含1的区间最少的,因此然而除了不包含1的区间外(除了[1,n]),其他区间都满足答案,因此这种情况下满足条件的区间一定是最多的。

n2时之间特判即可

//
// Created by lv_shen on 2023/7/11.
//

#include <bits/stdc++.h>

#define endl "\n"
using namespace std;
typedef long long LL;
typedef unsigned long long u64;
typedef pair<int, int> PII;
const int N = 2e5 + 110;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;



void solve ()
{
	int n;
	cin >> n;
	int a[N];
	if (n == 1) {
		cout << 1 << endl;
		return;
	}
	else if (n == 2) {
		cout << "1 2" << endl;
		return;
	}

	for (int i = 1; i <= n; i++) {
		a[i] = i;
	}

	swap (a[2], a[1]);
	swap (a[3], a[n]);
	swap (a[2], a[n / 2 + 1]);

	for (int i = 1; i <= n; i++) {
		cout << a[i] << " \n"[i == n];
	}
}

int main ()
{
	std::ios::sync_with_stdio (false);
	std::cin.tie (nullptr);
	std::cout.tie (nullptr);

	int t;
	cin >> t;
	get_primes (2e5 + 100);
	while (t--) {
		solve ();
	}

	return 0;
}

C. Particles

原题链接:Problem - C - Codeforces

题意:有一个长度为n的数组,每次可以删除一个元素,并且合并这个元素两边的元素,最后剩下的元素最大是多少

题解:可以发现,最后剩下的那个元素要么全部是由一开始的偶数序列的元素组成的,要么全部是由奇数序列的元素组成的,因此我们只需要分别考虑奇数位的正数和偶数位的正数即可。当所有元素都小于0时需要特判

//
// Created by lv_shen on 2023/7/11.
//

#include <bits/stdc++.h>

#define endl "\n"
using namespace std;
typedef long long LL;
typedef unsigned long long u64;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;

LL a[N], s[N];

void solve ()
{
	int n;
	cin >> n;
	bool f = false;
	LL j = 0, o = 0;
	LL maxx = -2e9;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		maxx = max (maxx, a[i]);
		if (a[i] > 0) {
			f = true;
			if (i & 1)j += a[i];
			else o += a[i];
		}
	}
	if (f)
		cout << max (j, o) << endl;
	else {
		cout << maxx << endl;
	}
}

int main ()
{
	std::ios::sync_with_stdio (false);
	std::cin.tie (nullptr);
	std::cout.tie (nullptr);

	int t;
	cin >> t;
	while (t--) {
		solve ();
	}

	return 0;
}

D. Row Major

原题链接:Problem - D - Codeforces

题意:用小写字母组成一个长度为n字符串,要求这个字符串排列成矩形后,有相邻的边的字母不能一样,且字母要尽可能少。

题解:直接找到不能整除n的的最小整数,这个整数就是这个字符串所包含的最少的字符种类数。假设这个数是c,再用这c种字符循环循环拼接成长度为n的字符串即可。下面证明这种构造方法的正确性。

首先这个最少的字符数肯定是大于等于c的,因为所有小于c的数都可以整除n。假设dn,那么第x个字母和第x+d个字母肯定不能相同。在这种构造方法中,两个相同的字母的位置是xx+kc(k1)。然而kc肯定是不等于d的。因为假设kc=d,然而dn,那么kcn,所以cn,这与前面假设的c是最小的不能整除n的整数(cn)矛盾。因此这种构造方法是可行的。

//
// Created by lv_shen on 2023/7/11.
//

#include <bits/stdc++.h>

#define endl "\n"
using namespace std;
typedef long long LL;
typedef unsigned long long u64;
typedef pair<int, int> PII;
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;

vector<int> g[N];
int c[N];

void solve ()
{
	int n;
	cin >> n;
	string s;
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		if (n % i) {
			ans = i;
			break;
		}
	}
	if (ans == 0) ans = n;
	//cout << ans << endl;
	for (int i = 0; i < ans; i++) {
		s += (char)('a' + i);
	}
	string ss;
	while (ss.size () < n) {
		ss += s;
	}
	cout << ss.substr (0, n) << endl;
}

int main ()
{
	std::ios::sync_with_stdio (false);
	std::cin.tie (nullptr);
	std::cout.tie (nullptr);

	int t;
	cin >> t;
	while (t--) {
		solve ();
	}

	return 0;
}
posted @   lvsc  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示