去我的Blog观看

修改时间: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题解的更多相关文章

  1. 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 ...

  2. 【47.95%】【codeforces 554C】Kyoya and Colored Balls

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 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 ...

  4. Codeforces554 C Kyoya and Colored Balls

    C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...

  5. 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 ...

  6. Kyoya and Colored Balls(组合数)

    Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  7. 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 ...

  8. 554C - Kyoya and Colored Balls

    554C - Kyoya and Colored Balls 思路:组合数,用乘法逆元求. 代码: #include<bits/stdc++.h> using namespace std; ...

  9. 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 ...

  10. Codeforces554C:Kyoya and Colored Balls(组合数学+费马小定理)

    Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...

随机推荐

  1. Linux(一)Linux简介、目录结构、网络配置与系统服务

    1 Linux简介 Linux基于Unix,是多用户分时系统 Ctrl + Alt + F2.F3...F6打开多个Linux Shell终端控制器:F1为图形化界面,终端为仿真器 2 Linux文件 ...

  2. Solon2 常用注解之 @ProxyComponent 用法说明

    在 Solon 提倡"克制"的原则下,托管组件分为: 普通组件 代理组件(即 @ProxyComponent 注解的类).代理的细节可以看下<动态代理的本质> . 之所 ...

  3. ts中报错信息收集

    1. 错误代码 参考:https://www.mmbyte.com/article/92849.html 1 state.localuserInfo = JSON.parse(localStorage ...

  4. 【解决方法】windos server 2019 在批量创建DNS的正向与反向记录时,提示报错: >Command failed: ERROR_ACCESS_DENIED 5 0x5

    目录-快速跳转 问题描述 原因分析: 解决方案: 附言: 问题描述 操作环境与场景: 在 VM 内 windos server 2019 在批量创建DNS的正向与反向记录时,提示报错: Command ...

  5. 2023-04-21:用go语言重写ffmpeg的metadata.c示例。

    2023-04-21:用go语言重写ffmpeg的metadata.c示例. 答案2023-04-21: 这段 Go 代码演示了如何使用 ffmpeg-go 库中的函数来读取多媒体文件元数据,包括视频 ...

  6. 2022-10-16:以下go语言代码输出什么?A:timed out;B:panic;C:没有任何输出。 package main import ( “context“ “fmt“

    2022-10-16:以下go语言代码输出什么?A:timed out:B:panic:C:没有任何输出. package main import ( "context" &quo ...

  7. 2021-12-04:公交路线。给你一个数组 routes ,表示一系列公交线路,其中每个 routes[i] 表示一条公交线路,第 i 辆公交车将会在上面循环行驶。 例如,路线 routes[0]

    2021-12-04:公交路线.给你一个数组 routes ,表示一系列公交线路,其中每个 routes[i] 表示一条公交线路,第 i 辆公交车将会在上面循环行驶. 例如,路线 routes[0] ...

  8. 聊聊Seata分布式解决方案AT模式的实现原理

    什么是Seata分布式事务解决方案 Seata是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务.为用户提供了AT.TCC.SAGA和XA事务模式,为用户打造一站式的分布式解决 ...

  9. 提升 Apache Hudi Upsert 性能的三个建议

    Apache Hudi 社区一直在快速发展,各公司正在寻找方法来利用其强大的功能来有效地摄取和管理大规模数据集. 每周社区都会收到一些常见问题,最常见的问题与 Hudi 如何执行更新插入有关,以确保以 ...

  10. UCOS II源码分析二

    最近大家都沉浸在找到实习的快乐中,自我充电的时间相对减少了,今天重拾一下ucosii的学习,记录如下: 上一篇文章分析了ucosii源码文件组织结构以及简单介绍了各个文件夹里对应文件的功能,要是对uc ...