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

相关文章

Oracle的to_date()函数详解

《Oracle的to_date()函数详解》Oracle的to_date()函数用于日期格式转换,需要注意Oracle中不区分大小写的MM和mm格式代码,应使用mi代替分钟,此外,Oracle还支持毫... 目录oracle的to_date()函数一.在使用Oracle的to_date函数来做日期转换二.日

Java实现任务管理器性能网络监控数据的方法详解

《Java实现任务管理器性能网络监控数据的方法详解》在现代操作系统中,任务管理器是一个非常重要的工具,用于监控和管理计算机的运行状态,包括CPU使用率、内存占用等,对于开发者和系统管理员来说,了解这些... 目录引言一、背景知识二、准备工作1. Maven依赖2. Gradle依赖三、代码实现四、代码详解五

SpringBoot基于MyBatis-Plus实现Lambda Query查询的示例代码

《SpringBoot基于MyBatis-Plus实现LambdaQuery查询的示例代码》MyBatis-Plus是MyBatis的增强工具,简化了数据库操作,并提高了开发效率,它提供了多种查询方... 目录引言基础环境配置依赖配置(Maven)application.yml 配置表结构设计demo_st

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.

Java调用Python代码的几种方法小结

《Java调用Python代码的几种方法小结》Python语言有丰富的系统管理、数据处理、统计类软件包,因此从java应用中调用Python代码的需求很常见、实用,本文介绍几种方法从java调用Pyt... 目录引言Java core使用ProcessBuilder使用Java脚本引擎总结引言python

Mysql 中的多表连接和连接类型详解

《Mysql中的多表连接和连接类型详解》这篇文章详细介绍了MySQL中的多表连接及其各种类型,包括内连接、左连接、右连接、全外连接、自连接和交叉连接,通过这些连接方式,可以将分散在不同表中的相关数据... 目录什么是多表连接?1. 内连接(INNER JOIN)2. 左连接(LEFT JOIN 或 LEFT

Java中ArrayList的8种浅拷贝方式示例代码

《Java中ArrayList的8种浅拷贝方式示例代码》:本文主要介绍Java中ArrayList的8种浅拷贝方式的相关资料,讲解了Java中ArrayList的浅拷贝概念,并详细分享了八种实现浅... 目录引言什么是浅拷贝?ArrayList 浅拷贝的重要性方法一:使用构造函数方法二:使用 addAll(

Java中switch-case结构的使用方法举例详解

《Java中switch-case结构的使用方法举例详解》:本文主要介绍Java中switch-case结构使用的相关资料,switch-case结构是Java中处理多个分支条件的一种有效方式,它... 目录前言一、switch-case结构的基本语法二、使用示例三、注意事项四、总结前言对于Java初学者

Linux内核之内核裁剪详解

《Linux内核之内核裁剪详解》Linux内核裁剪是通过移除不必要的功能和模块,调整配置参数来优化内核,以满足特定需求,裁剪的方法包括使用配置选项、模块化设计和优化配置参数,图形裁剪工具如makeme... 目录简介一、 裁剪的原因二、裁剪的方法三、图形裁剪工具四、操作说明五、make menuconfig

JAVA利用顺序表实现“杨辉三角”的思路及代码示例

《JAVA利用顺序表实现“杨辉三角”的思路及代码示例》杨辉三角形是中国古代数学的杰出研究成果之一,是我国北宋数学家贾宪于1050年首先发现并使用的,:本文主要介绍JAVA利用顺序表实现杨辉三角的思... 目录一:“杨辉三角”题目链接二:题解代码:三:题解思路:总结一:“杨辉三角”题目链接题目链接:点击这里