去我的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. SpringBoot入门(二):Controller的使用

    Controller中注解的使用:   @Controller   ●该注解用来响应页面,必须配合模板来使用   @RestController ●该注解可以直接响应字符串,返回的类型为JSON格式 ...

  2. 还在玩传统终端,不妨来试试全新 AI 终端 Warp

    壹 ❀ 引 最近一段时间,AI领域如同雨后春笋般开始猛烈生长,processon,sentry,一些日常使用的工具都在积极接入AI,那么正好借着AI的风头,今天给大家推荐一款非常不错的智能终端 war ...

  3. vant中van-dialog组件点击确认按钮禁止弹窗自动关闭

    1.在van-dialog组件中添加 before-close 属性, 2.定义该方法 newGroupBefColse(action, done) { if (action == 'confirm' ...

  4. 2023-03-18:给定一个长度n的数组,每次可以选择一个数x, 让这个数组中所有的x都变成x+1,问你最少的操作次数, 使得这个数组变成一个非降数组。 n <= 3 * 10^5, 0 <= 数值

    2023-03-18:给定一个长度n的数组,每次可以选择一个数x, 让这个数组中所有的x都变成x+1,问你最少的操作次数, 使得这个数组变成一个非降数组. n <= 3 * 10^5, 0 &l ...

  5. 2021-08-01:如果只给定一个二叉树前序遍历数组pre和中序遍历数组in,能否不重建树,而直接生成这个二叉树的后序数组并返回。已知二叉树中没有重复值。

    2021-08-01:如果只给定一个二叉树前序遍历数组pre和中序遍历数组in,能否不重建树,而直接生成这个二叉树的后序数组并返回.已知二叉树中没有重复值. 福大大 答案2021-08-01: 先序遍 ...

  6. 2021-09-06:给表达式添加运算符。给定一个仅包含数字 0-9 的字符串 num 和一个目标值整数 target ,在 num 的数字之间添加 二元 运算符(不是一元)+、- 或 * ,返回所有

    2021-09-06:给表达式添加运算符.给定一个仅包含数字 0-9 的字符串 num 和一个目标值整数 target ,在 num 的数字之间添加 二元 运算符(不是一元)+.- 或 * ,返回所有 ...

  7. 6R机械臂运动规划及仿真

    博客地址:https://www.cnblogs.com/zylyehuo/ 参考链接 Moveit!机械臂控制 文件下载-古月ROS教程视频配套资料 解决Could not find a packa ...

  8. vue全家桶进阶之路31:Vue3 数据和方法的双向绑定ref、reactive、toRefs

    ref 在 Vue 3 中,你可以使用 setup 函数来定义组件的数据和方法.在 setup 函数中,你可以使用 ref.reactive 和 computed 等 Vue 3 的响应式 API 来 ...

  9. 分布式协调服务之Zookeeper

    1. 认识Zookeeper ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用 ...

  10. OCR -- 文本检测 - 训练DB文字检测模型

    百度飞桨(PaddlePaddle) - PP-OCRv3 文字检测识别系统 预测部署简介与总览 百度飞桨(PaddlePaddle) - PP-OCRv3 文字检测识别系统 Paddle Infer ...