Codeforces Round #638 (Div. 2)---题目+详解+代码(A、B、C)

2023-11-07 06:08

本文主要是介绍Codeforces Round #638 (Div. 2)---题目+详解+代码(A、B、C),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • A. Phoenix and Balance
  • B. Phoenix and Beauty
  • C. Phoenix and Distribution

A. Phoenix and Balance

来源:http://codeforces.com/contest/1348/problem/A

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Phoenix has n coins with weights 21,22,…,2n. He knows that n is even.

He wants to split the coins into two piles such that each pile has exactly n2 coins and the difference of weights between the two piles is minimized. Formally, let a denote the sum of weights in the first pile, and b denote the sum of weights in the second pile. Help Phoenix minimize |a−b|, the absolute value of a−b.

Input
The input consists of multiple test cases. The first line contains an integer t (1≤t≤100) — the number of test cases.

The first line of each test case contains an integer n (2≤n≤30; n is even) — the number of coins that Phoenix has.

Output
For each test case, output one integer — the minimum possible difference of weights between the two piles.

Example
inputCopy
2
2
4
outputCopy
2
6
Note
In the first test case, Phoenix has two coins with weights 2 and 4. No matter how he divides the coins, the difference will be 4−2=2.

In the second test case, Phoenix has four coins of weight 2, 4, 8, and 16. It is optimal for Phoenix to place coins with weights 2 and 16 in one pile, and coins with weights 4 and 8 in another pile. The difference is (2+16)−(4+8)=6.

题意:
给你一个整数n,将2的1次-n次方作为一个数组,将其分成两部分,使两部分的和的差最小

思路:
很明显是贪心的题目,将最大的一个和前n/2-1个小的放在一起,其他的放在一起就ok了,注意提前定义数组储存2的n次幂

#include<iostream>
using namespace std;
typedef long long ll;
ll s[33];
int x(int n)
{if (n == 1) return 2;else return 2 * x(n - 1);
}
int main()
{for (int i = 1; i <= 30; i++)//存储s[i] += s[i - 1] + x(i);int t;cin >> t;while (t--){int n;cin >> n;if (n == 2) cout << 2 << endl;else{int t = n / 2 - 1;cout << (s[t] + x(n) - (s[n - 1] - s[t])) << endl;}}return 0;
}

B. Phoenix and Beauty

来源:http://codeforces.com/contest/1348/problem/B

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Phoenix loves beautiful arrays. An array is beautiful if all its subarrays of length k have the same sum. A subarray of an array is any sequence of consecutive elements.

Phoenix currently has an array a of length n. He wants to insert some number of integers, possibly zero, into his array such that it becomes beautiful. The inserted integers must be between 1 and n inclusive. Integers may be inserted anywhere (even before the first or after the last element), and he is not trying to minimize the number of inserted integers.

Input
The input consists of multiple test cases. The first line contains an integer t (1≤t≤50) — the number of test cases.

The first line of each test case contains two integers n and k (1≤k≤n≤100).

The second line of each test case contains n space-separated integers (1≤ai≤n) — the array that Phoenix currently has. This array may or may not be already beautiful.

Output
For each test case, if it is impossible to create a beautiful array, print -1. Otherwise, print two lines.

The first line should contain the length of the beautiful array m (n≤m≤104). You don’t need to minimize m.

The second line should contain m space-separated integers (1≤bi≤n) — a beautiful array that Phoenix can obtain after inserting some, possibly zero, integers into his array a. You may print integers that weren’t originally in array a.

If there are multiple solutions, print any. It’s guaranteed that if we can make array a beautiful, we can always make it with resulting length no more than 104.

Example
inputCopy
4
4 2
1 2 2 1
4 3
1 2 2 1
3 2
1 2 3
4 4
4 3 4 2
outputCopy
5
1 2 1 2 1
4
1 2 2 1
-1
7
4 3 2 1 4 3 2
Note
In the first test case, we can make array a beautiful by inserting the integer 1 at index 3 (in between the two existing 2s). Now, all subarrays of length k=2 have the same sum 3. There exists many other possible solutions, for example:

2,1,2,1,2,1
1,2,1,2,1,2
In the second test case, the array is already beautiful: all subarrays of length k=3 have the same sum 5.

In the third test case, it can be shown that we cannot insert numbers to make array a beautiful.

In the fourth test case, the array b shown is beautiful and all subarrays of length k=4 have the same sum 10. There exist other solutions also.

题意:
给出一个n个数的序列a,如何可以插入几个数使其长度为k的子序列和相同。例如,n=4,k=2,a = 1 2 2 1,一个答案是1 2 1 2 1,和都为3

思路:
为了使某个数组的k美观,该数组必须是周期为k的周期。 如果数组a中存在超过k个不同的数字,则没有答案,打印-1(因为数组不能以周期k为周期)
具体实现见代码:

#include <cstdio>
#include <algorithm>
using namespace std;
int a[105], vis[105] = { 0 }, c[105], cn;
int main() 
{int t, i, b, n, k, cnt, j;scanf("%d", &t);while (t--) {scanf("%d%d", &n, &k);for (i = 1; i <= n; i++){scanf("%d", &a[i]);vis[a[i]] = 1;}//vis[i]=1表示,数据1出现过cnt = 0;for (i = 1; i <= 100; i++)if (vis[i])cnt++;//记录数组中的值不雷同的种类数量if (cnt > k) { printf("-1\n"); continue; }//数组中的值不雷同的种类数量,过多if (cnt < k)//数组中的值不雷同的种类数量,过少for (i = 1; i <= 100; i++)if (!vis[i]) {vis[i] = 1;cnt++;//添加新值到周期中if (cnt == k)break;}cn = 0;for (i = 1; i <= 100; i++)if (vis[i]==1)c[++cn] = i;//记录一个周期内,数组的值printf("%d\n", n * k);for (i = 1; i <= n; i++)//打印数据for (j = 1; j <= k; j++)printf("%d ", c[j]);printf("\n");}return 0;
}

C. Phoenix and Distribution

来源:http://codeforces.com/contest/1348/problem/C

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Phoenix has a string s consisting of lowercase Latin letters. He wants to distribute all the letters of his string into k non-empty strings a1,a2,…,ak such that every letter of s goes to exactly one of the strings ai. The strings ai do not need to be substrings of s. Phoenix can distribute letters of s and rearrange the letters within each string ai however he wants.

For example, if s= baba and k=2, Phoenix may distribute the letters of his string in many ways, such as:

ba and ba
a and abb
ab and ab
aa and bb
But these ways are invalid:

baa and ba
b and ba
baba and empty string (ai should be non-empty)
Phoenix wants to distribute the letters of his string s into k strings a1,a2,…,ak to minimize the lexicographically maximum string among them, i. e. minimize max(a1,a2,…,ak). Help him find the optimal distribution and print the minimal possible value of max(a1,a2,…,ak).

String x is lexicographically less than string y if either x is a prefix of y and x≠y, or there exists an index i (1≤i≤min(|x|,|y|)) such that xi < yi and for every j (1≤j<i) xj=yj. Here |x| denotes the length of the string x.

Input
The input consists of multiple test cases. The first line contains an integer t (1≤t≤1000) — the number of test cases. Each test case consists of two lines.

The first line of each test case consists of two integers n and k (1≤k≤n≤105) — the length of string s and the number of non-empty strings, into which Phoenix wants to distribute letters of s, respectively.

The second line of each test case contains a string s of length n consisting only of lowercase Latin letters.

It is guaranteed that the sum of n over all test cases is ≤105.

Output
Print t answers — one per test case. The i-th answer should be the minimal possible value of max(a1,a2,…,ak) in the i-th test case.

Example
inputCopy
6
4 2
baba
5 2
baacb
5 3
baacb
5 3
aaaaa
6 4
aaxxzz
7 1
phoenix
outputCopy
ab
abbc
b
aa
x
ehinopx
Note
In the first test case, one optimal solution is to distribute baba into ab and ab.

In the second test case, one optimal solution is to distribute baacb into abbc and a.

In the third test case, one optimal solution is to distribute baacb into ac, ab, and b.

In the fourth test case, one optimal solution is to distribute aaaaa into aa, aa, and a.

In the fifth test case, one optimal solution is to distribute aaxxzz into az, az, x, and x.

In the sixth test case, one optimal solution is to distribute phoenix into ehinopx.
题意:
给你n个字符,要求你将其分为k份,每份个数至少有1个,使得新组合的字符串中字典序最大的字符串的字典序尽可能小,打印这个字符串。
思路:
首先把s排序,
如果t=1,直接返回排好的s
如果s第t位和第1位不同,答案就是s[t],如t=4,可以分成a…,a…,a…,b把剩下的东西分给a…就好了
如果相同,有两种情况,剩下的字母全一样,那么平分,如aabbbb分成abb,abb
只要有一个不一样,如aabbbbc,分成a,abbbbc,这个b或者c分给前面的a都不合适,因为会把c位置提前字典序变大
代码:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
char s[N];
int main() {int T;cin >> T;while (T--) {int n, t;cin >> n >> t;cin >> s + 1;sort(s + 1, s + n + 1);if (t == 1) {cout << s + 1 << "\n";continue;}if (s[t] != s[1]) cout << s[t] << "\n";else {if (s[t + 1] == s[n]) {cout << s[t];int cnt = (n - t) / t;if ((n - t) % t) cnt++;while (cnt--) cout << s[t + 1];puts("");}else cout << s + t << "\n";}}return 0;
}

如有错误,欢迎各路da lao指正

这篇关于Codeforces Round #638 (Div. 2)---题目+详解+代码(A、B、C)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/361741

相关文章

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

一文深入详解Python的secrets模块

《一文深入详解Python的secrets模块》在构建涉及用户身份认证、权限管理、加密通信等系统时,开发者最不能忽视的一个问题就是“安全性”,Python在3.6版本中引入了专门面向安全用途的secr... 目录引言一、背景与动机:为什么需要 secrets 模块?二、secrets 模块的核心功能1. 基

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五