Codeforces Round #775
题目链接
Codeforces Round #775
B. Game of Ball Passing
Daniel is watching a football team playing a game during their training session. They want to improve their passing skills during that session.
The game involves players, making multiple passes towards each other. Unfortunately, since the balls were moving too fast, after the session Daniel is unable to know how many balls were involved during the game. The only thing he knows is the number of passes delivered by each player during all the session.
Find the minimum possible amount of balls that were involved in the game.
Input
There are several test cases in the input data. The first line contains a single integer the number of test cases. This is followed by the test cases description.
The first line of each test case contains one integer the number of players.
The second line of the test case contains a sequence of integers , where is the number of passes delivered by the -th player.
It is guaranteed that the sum of over all test cases doesn't exceed .
Output
For each test case print a single integer - the answer to the problem.
Example
input
output
解题思路
思维
考虑传球最多的次数 ,类似于填数:,如果 ,则一定可以将 填满,即只需要一个球,反之,尽量填数:,填不了则还要形成一条链,共 条链,另外考虑全为 的特殊情况
- 时间复杂度:
代码
C. Weird Sum
Egor has a table of size , with lines numbered from 1 to and columns numbered from 1 to . Each cell has a color that can be presented as an integer from 1 to .
Let us denote the cell that lies in the intersection of the -th row and the -th column as . We define the manhattan distance between two cells and as the length of a shortest path between them where each consecutive cells in the path must have a common side. The path can go through cells of any color. For example, in the table the manhattan distance between and is 3 , one of the shortest paths is the following: .
Egor decided to calculate the sum of manhattan distances between each pair of cells of the same color. Help him to calculate this sum.
Input
The first line contains two integers and - number of rows and columns in the table.
Each of next lines describes a row of the table. The -th line contains integers colors of cells in the -th row.
Output
Print one integer - the the sum of manhattan distances between each pair of cells of the same color.
input
output
input
output
input
output
解题思路
思维
考虑每个点的贡献,且 是独立的,互不影响,两者可以单独考虑,将颜色分开,对于一种颜色,以横坐标为例:如果前面有 个,后面有 个,则其贡献为 ,当然可以掉优化排序
- 时间复杂度:
代码
- 未优化
- 优化
__EOF__

本文链接:https://www.cnblogs.com/zyyun/p/15976097.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!