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
题意: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
题意:给定一个n,要求输入一个n的全排列。这个全排列要求要满足mex(a[l]...a[r])为质数的(l,r)数对的数量最多
题解:在
因为要让mex是一个质数,必须要让这个区间内的数包含1,因此考虑将1放到中间,这样包含1的区间会更多(为什么中间是最多的呢?假设放在位置
//
// 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
题意:有一个长度为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
题意:用小写字母组成一个长度为n字符串,要求这个字符串排列成矩形后,有相邻的边的字母不能一样,且字母要尽可能少。
题解:直接找到不能整除n的的最小整数,这个整数就是这个字符串所包含的最少的字符种类数。假设这个数是
首先这个最少的字符数肯定是大于等于
//
// 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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话