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

相关文章

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

Java Predicate接口定义详解

《JavaPredicate接口定义详解》Predicate是Java中的一个函数式接口,它代表一个判断逻辑,接收一个输入参数,返回一个布尔值,:本文主要介绍JavaPredicate接口的定义... 目录Java Predicate接口Java lamda表达式 Predicate<T>、BiFuncti

详解如何通过Python批量转换图片为PDF

《详解如何通过Python批量转换图片为PDF》:本文主要介绍如何基于Python+Tkinter开发的图片批量转PDF工具,可以支持批量添加图片,拖拽等操作,感兴趣的小伙伴可以参考一下... 目录1. 概述2. 功能亮点2.1 主要功能2.2 界面设计3. 使用指南3.1 运行环境3.2 使用步骤4. 核

一文详解JavaScript中的fetch方法

《一文详解JavaScript中的fetch方法》fetch函数是一个用于在JavaScript中执行HTTP请求的现代API,它提供了一种更简洁、更强大的方式来处理网络请求,:本文主要介绍Jav... 目录前言什么是 fetch 方法基本语法简单的 GET 请求示例代码解释发送 POST 请求示例代码解释

详解nginx 中location和 proxy_pass的匹配规则

《详解nginx中location和proxy_pass的匹配规则》location是Nginx中用来匹配客户端请求URI的指令,决定如何处理特定路径的请求,它定义了请求的路由规则,后续的配置(如... 目录location 的作用语法示例:location /www.chinasem.cntestproxy

CSS will-change 属性示例详解

《CSSwill-change属性示例详解》will-change是一个CSS属性,用于告诉浏览器某个元素在未来可能会发生哪些变化,本文给大家介绍CSSwill-change属性详解,感... will-change 是一个 css 属性,用于告诉浏览器某个元素在未来可能会发生哪些变化。这可以帮助浏览器优化

Python基础文件操作方法超详细讲解(详解版)

《Python基础文件操作方法超详细讲解(详解版)》文件就是操作系统为用户或应用程序提供的一个读写硬盘的虚拟单位,文件的核心操作就是读和写,:本文主要介绍Python基础文件操作方法超详细讲解的相... 目录一、文件操作1. 文件打开与关闭1.1 打开文件1.2 关闭文件2. 访问模式及说明二、文件读写1.

详解C++中类的大小决定因数

《详解C++中类的大小决定因数》类的大小受多个因素影响,主要包括成员变量、对齐方式、继承关系、虚函数表等,下面就来介绍一下,具有一定的参考价值,感兴趣的可以了解一下... 目录1. 非静态数据成员示例:2. 数据对齐(Padding)示例:3. 虚函数(vtable 指针)示例:4. 继承普通继承虚继承5.

前端高级CSS用法示例详解

《前端高级CSS用法示例详解》在前端开发中,CSS(层叠样式表)不仅是用来控制网页的外观和布局,更是实现复杂交互和动态效果的关键技术之一,随着前端技术的不断发展,CSS的用法也日益丰富和高级,本文将深... 前端高级css用法在前端开发中,CSS(层叠样式表)不仅是用来控制网页的外观和布局,更是实现复杂交

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将