CF1728A Colored Balls: Revisited题解
修改时间:2022/9/11修改了格式与标点
修改时间:2022/9/13修改了个别不严谨的语句
题目大意
有 \(n\) 种颜色的球,颜色为 \(i\) 的球为 \(cnt_i\) 个(\(cnt_1+cnt_2+\dots+cnt_n\) 为奇数)。每次从球堆中取出 \(2\) 个颜色不相同的球,问最后可能剩下哪种颜色的球(输出任意一种即可)。
题目分析
其实只要找出序列中最大值所在位置 \(i\) 即可,那就是最后剩下的一种球。
理论证明(可以不看)
将 \(cnt\) 数组从小到大排序,先同时拿排在第一个位置和第二个位置的球,直到第一个位置没有球了(也就是排序后的第一个颜色被拿完了)。
此时第二个位置应该还有球,再同时拿第二个位置和第三个位置的球,直到第二个位置没有球了。
第三个位置应该还有球。
以此类推,最后在第 \(n-1\) 个位置的球拿完时,第 \(n\) 个位置的球一定是唯一的一种颜色的球。
常见问题
Q1:如果只有两个数字且个数相同怎么办?
A1:那它们的和为偶数违背了题意(\(cnt_1+cnt_2+\dots+cnt_n\) 为奇数)。
Q2:会不会到最后两个位置时,它们的数字相同?
A2:不可能。因为在两个数字的时候不成立(已证明)。那在多个数字的时候,第 \(n-1\) 个数字已经和第 \(n-2\) 个数字消掉一部分了,如果此时 \(cnt_{n-1}=cnt_{n-2}\),那么原先的 \(cnt_{n-1}\) 一定大于 \(cnt_{n-2}\),与排序矛盾。
代码实现
#include <bits/stdc++.h>
using namespace std;
const int N=25; //最大个数为25个
int T,n; //数据组数与数据个数
int a[N]; //存放数字
void solve()
{
int color_max=1; //记录最大值的那一位
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=2;i<=n;i++)
if(a[color_max]<a[i])//如果当前值比最大值还要大,更新最大值
color_max=i;
printf("%d\n",color_max);//输出最大值所在位置
}
int main()
{
scanf("%d",&T);
while(T--)solve();
return 0;
}
如果您觉得内容对您有用,请点个赞!
CF1728A Colored Balls: Revisited题解的更多相关文章
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合
C. Kyoya and Colored Balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- 【47.95%】【codeforces 554C】Kyoya and Colored Balls
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- CF-weekly4 F. Kyoya and Colored Balls
https://codeforces.com/gym/253910/problem/F F. Kyoya and Colored Balls time limit per test 2 seconds ...
- Codeforces554 C Kyoya and Colored Balls
C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...
- codeforces 553A . Kyoya and Colored Balls 组合数学
Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...
- Kyoya and Colored Balls(组合数)
Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- C. Kyoya and Colored Balls(Codeforces Round #309 (Div. 2))
C. Kyoya and Colored Balls Kyoya Ootori has a bag with n colored balls that are colored with k diffe ...
- 554C - Kyoya and Colored Balls
554C - Kyoya and Colored Balls 思路:组合数,用乘法逆元求. 代码: #include<bits/stdc++.h> using namespace std; ...
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls
Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...
- Codeforces554C:Kyoya and Colored Balls(组合数学+费马小定理)
Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...
随机推荐
- GPT-4:思考的曙光还是数据的缩影?
海盗分金,GPT-4初露锋芒 GPT系列模型横空出世后,其是否真实具有思考和推理的能力一直被业界关注.GPT-3.5在多条狗问题和海盗分金问题上表现糟糕.GPT-4在这两个谜题上给出的答案令人惊喜,甚 ...
- Mysql中如果建立了索引,索引所占的空间随着数据量增长而变大,这样无论写入还是查询,性能都会有所下降,怎么处理?
索引所占空间的增长确实会对MySQL数据库的写入性能和查询性能造成影响,这主要是由于索引数据过多时会导致磁盘I/O操作变得非常频繁,从而使性能下降.为此,可以采取以下几种方式来减缓这种影响: 1. 限 ...
- SqlServer查看表结构
SELECT CASE WHEN col.colorder = 1 THEN obj.name ELSE '' END AS 表名 ,CASE WHEN col.colorder=1 then isn ...
- Vue中实现数据列表无缝轮播
类似这种滚动轮播效果 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta char ...
- YOLO1论文中文版
文章目录 YOLO1中文版 摘要 1. 引言 2. 统一检测 2.1 网络设计 2.2 训练 2.3 推断 2.4 YOLO的限制 3. 与其它检测系统的比较 4. 实验 4. 1 与其它实时系统的比 ...
- selenium控制文件下载位置
selenium控制文件下载位置 我们在自动化下载文件的时候势必存在一种需求: 通过chrome将文件保存到指定位置 1. google窗口实现 配置'prefs'将文件下载到指定位置, 并通过判 ...
- 使用 shell 脚本自动申请进京证 (六环外) —— debug 过程
问题现象 用 shell 脚本写了一个自动办理六环外进京证的工具 <使用 shell 脚本自动申请进京证 (六环外)>,然而运行这个脚本总是返回以下错误信息: { "msg&qu ...
- selenium web控件的交互进阶
Action ActionChains: 执行PC端的鼠标点击,双击,右键,拖曳等事件 TouchActions: 模拟PC和移动端的点击,滑动,拖曳,多点触控等多种手势操作 动作链接 ActionC ...
- Vue使用:style动态给css中某样式赋值
template中 <span class="successOrError" :style="{'--fontColor':"green"}&q ...
- 2022-10-27:设计一个数据结构,有效地找到给定子数组的 多数元素 。 子数组的 多数元素 是在子数组中出现 threshold 次数或次数以上的元素。 实现 MajorityChecker 类
2022-10-27:设计一个数据结构,有效地找到给定子数组的 多数元素 . 子数组的 多数元素 是在子数组中出现 threshold 次数或次数以上的元素. 实现 MajorityChecker 类 ...