当前位置: 首页 > news >正文

题解:AtCoder AT_awc0085_a Tournament Elimination Round

本文分享的必刷题目是从蓝桥云课洛谷AcWing等知名刷题平台精心挑选而来,并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构,旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。

欢迎大家订阅我的专栏:算法题解:C++与Python实现!

附上汇总贴:算法竞赛备考冲刺必刷题(C++) | 汇总


【题目来源】

AtCoder:A - Tournament Elimination Round (atcoder.jp)

【题目描述】

A single-elimination tournament is held withN NNplayers. Here,N NNis a power of2 22.

Each player is assigned a number from1 11toN NN, and the strength of playeri iiisS i S_iSi. All players have distinct strengths, and in any match, the player with the greater strength value always wins. A player who loses is eliminated from the tournament, and matches continue until a single champion is determined.

SinceN NNis a power of2 22, the tournament consists of exactlyR = log ⁡ 2 N R = \log_2 NR=log2Nrounds. The matchups for each round are determined as follows:

  • Round1 11:Player2 i − 1 2i-12i1and player2 i 2i2iplay against each other. This is called matchi iiof round1 11(i = 1 , 2 , … , N / 2 i = 1, 2, \ldots, N/2i=1,2,,N/2).
  • Roundr rr(r ≥ 2 r \geq 2r2):The winner of match2 j − 1 2j-12j1and the winner of match2 j 2j2jfrom the previous round (roundr − 1 r-1r1) play against each other. This is called matchj jjof roundr rr(j = 1 , 2 , … , N / 2 r j = 1, 2, \ldots, N/2^rj=1,2,,N/2r).

Match numbers within each round are numbered starting from1 11and are reassigned for each round. In roundR RR, there is only one match, and its winner is the champion.

For each player, determine the round number in which that player is defeated. However, since the champion is never defeated, output0 00for the champion. Rounds are numbered starting from1 11.

一场单败淘汰赛有N NN名选手参加。这里,N NN2 22的幂。

每位选手被分配一个从1 11N NN的编号,选手i ii的强度为S i S_iSi。所有选手的强度互不相同,在任何一场比赛中,强度值更大的选手总是获胜。输掉比赛的选手被淘汰出局,比赛继续直到决出唯一的冠军。

由于N NN2 22的幂,比赛恰好有R = log ⁡ 2 N R = \log_2 NR=log2N轮。每一轮的对阵安排如下:

  • 1 11:选手2 i − 1 2i-12i1和选手2 i 2i2i相互对战。这被称为第1 11轮的第i ii场比赛(i = 1 , 2 , … , N / 2 i = 1, 2, \ldots, N/2i=1,2,,N/2)。
  • r rr轮(r ≥ 2 r \geq 2r2:上一轮(第r − 1 r-1r1轮)第2 j − 1 2j-12j1场比赛的胜者和第2 j 2j2j场比赛的胜者相互对战。这被称为第r rr轮的第j jj场比赛(j = 1 , 2 , … , N / 2 r j = 1, 2, \ldots, N/2^rj=1,2,,N/2r)。

每轮内部的比赛编号从1 11开始,并且每轮重新编号。在第R RR轮,只有一场比赛,其胜者即为冠军。

对于每位选手,确定该选手在哪一轮被淘汰。但是,由于冠军从未被淘汰,为冠军输出0 00。轮次编号从1 11开始。

【输入】

N NN
S 1 S_1S1S 2 S_2S2… \ldotsS N S_NSN

The first line gives the number of playersN NN.

The second line gives the strengthsS 1 , S 2 , … , S N S_1, S_2, \ldots, S_NS1,S2,,SNof each player, separated by spaces.

【输出】

OutputN NNintegers separated by spaces on a single line. Thei ii-th value is the round number in which playeri iiis defeated. For the champion, output0 00.

【输入样例】

4 1 4 3 2

【输出样例】

1 0 2 1

【算法标签】

#模拟

【代码详解】

#include<bits/stdc++.h>usingnamespacestd;constintN=530005;// 定义最大数组长度intn;// 选手数量structNode{intid,s;// 选手编号id,选手实力s}a[N],b[N];// 当前轮数组a,下一轮数组bintrd[N];// 记录每个选手被淘汰的轮数intmain()// 主函数{cin>>n;// 输入选手数量for(inti=1;i<=n;i++)// 初始化所有选手{a[i].id=i;// 设置选手编号cin>>a[i].s;// 输入选手实力}intcur1=n;// 当前轮参赛选手数量intcnt=0;// 当前轮数计数器while(true)// 模拟淘汰赛{cnt++;// 轮数加1intcur2=0;// 下一轮选手数量for(inti=1;i<=cur1;i+=2)// 两两比较{if(a[i].s>a[i+1].s)// 如果左边选手更强{cur2++;// 下一轮选手数量加1b[cur2].id=a[i].id,b[cur2].s=a[i].s;// 左边选手晋级rd[a[i+1].id]=cnt;// 右边选手被淘汰}else// 如果右边选手更强{cur2++;// 下一轮选手数量加1b[cur2].id=a[i+1].id,b[cur2].s=a[i+1].s;// 右边选手晋级rd[a[i].id]=cnt;// 左边选手被淘汰}}cur1=cur2;// 更新当前轮选手数量memcpy(a,b,sizeof(b));// 将下一轮数据复制到当前轮if(cur2==1)break;// 如果只剩1名选手,比赛结束}for(inti=1;i<=n;i++)// 输出结果cout<<rd[i]<<" ";// 输出每个选手被淘汰的轮数cout<<endl;// 换行return0;// 程序正常结束}

【运行结果】

http://www.jsqmd.com/news/971560/

相关文章:

  • ESP32玩转OLED:除了显示文字,还能用Img2Lcd自制像素画和动画
  • 项目实训开发日志(八)
  • 告别ADE_L的繁琐:用Cadence 617的ADE_XL,5分钟搞定两级运放的多工艺角仿真
  • 亚马逊商品图片批量采集系统:多变体SKU图提取与自动分类
  • 从Linux内核源码nand_ecc.c看ECC校验:如何用空间换时间优化嵌入式存储性能
  • SAP(ERP) 分包Subcontracting的MRP逻辑解析
  • CarPlay 让驾驶更便捷:多款实用车载应用推荐,让行程轻松顺利
  • 2026年亿路交通设施口碑如何 - mypinpai
  • 深入HDFS加密区域:图解EZ Key、DEK与KMS,搞懂数据‘套娃’加密原理
  • 一个利用AI现有能力快速流转客户续单量下降的真实案例
  • Android 开发中的 Logcat 日志过滤与分析
  • 2026年尼日利亚空运清关行排名,鹏达运通性价比高 - mypinpai
  • 2026年 陕西家居维修全攻略榜单:瓷砖/墙面/水电/门窗/家具翻新改色/贴膜/防水堵漏,专业服务与匠心品质口碑之选 - 品牌发掘
  • 百度网盘秒传脚本完整指南:3步实现永久文件分享
  • 51单片机项目避坑指南:深入理解TCON的ITx位与TMOD的GATE位(以红外遥控/按键检测为例)
  • 学习周报四十八
  • 数字孪生+AI:打造智慧林场
  • AI 短视频自动流水线搭建实战:ComfyUI + FLUX + HyperFrames 从配置到出片
  • 大千万级文档 RAG,这 11 个步骤把幻觉压到极低
  • 数据结构期末复习:第三章 栈和队列(选择题25道+判断题18道+程序题6道)进栈/出栈/循环队列/链队/递归
  • 如何让数据科学在GPU上“飞”起来:从龟速到百倍加速的实战指南
  • 深入浅出图解HDFS透明加密:从EZ Key到EDEK,一次搞懂数据安全核心架构
  • 深度专栏 | 粉碎感官玄学:精品可可的冷酷重构与物理变量
  • 选球场围网加工厂?2026年持盈金属丝网实力上榜 - mypinpai
  • 用手机App Inventor做个遥控器:5分钟实现蓝牙控制Arduino LED(HC-42模块实战)
  • HarmonyOS FIDO 免密认证:让你的APP支持用指纹和人脸代替密码
  • dill:扩展 Python pickle 的序列化库
  • 2026年AI中转站大全|API聚合平台横评推荐:从企业级高可用到开源,含稳定性对比+成本省钱技巧+避坑防骗指南(实测Token173/CatRouter/非线智能/OpenRouter/七牛云AI等
  • Linux网络管理
  • NSK极速滚珠丝杠USFC 2040-6技术手册