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

题解:AtCoder AT_awc0003_b Line of Handshakes

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

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

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


【题目来源】

AtCoder:B - Line of Handshakes

【题目描述】

Takahashi is observing an interesting phenomenon at a party venue.
高桥正在派对会场观察一个有趣的现象。

N NNparticipants are standing in a line, all facing the same direction (to the right), and are numbered1 , 2 , … , N 1, 2, \ldots, N1,2,,Nfrom left to right. Each participant is wearing a glove on their left hand and right hand (from their own perspective). The color of each glove is either navy or white. In the input, navy is represented byN(Navy) and white is represented byS(Snow).
N NN名参与者站成一排,全部面向同一方向(右侧),从左到右编号为1 , 2 , … , N 1, 2, …, N1,2,,N。每名参与者在其左手和右手上各戴有一只手套(从参与者自身视角)。每只手套的颜色为深蓝色或白色。在输入中,深蓝色用N(Navy)表示,白色用S(Snow)表示。

>Note:The characterNrepresenting the navy color and the integerN NNrepresenting the number of participants are different things. Please be careful not to confuse them.

注意:表示深蓝色的字符N与表示参与者数量的整数N NN是不同的概念。请注意不要混淆。

The color of participanti ii’s left-hand glove is given asL i L_iLi, and the color of their right-hand glove is given asR i R_iRi.
参与者i ii的左手手套颜色由L i L_iLi给出,其右手手套颜色由R i R_iRi给出。

Since all participants are standing in a line facing the same direction, when two adjacent participants shake hands, the right hand of the left participant (the one with the smaller number) and the left hand of the right participant (the one with the larger number) touch each other. If the colors of the two gloves that touch are the same (both navy or both white), this is called anawkward handshake.
由于所有参与者站成一排且面向同一方向,当两名相邻参与者握手时,左侧参与者(编号较小者)的右手与右侧参与者(编号较大者)的左手会相互接触。如果接触的两只手套颜色相同(均为深蓝色或均为白色),则称为尴尬握手。

Takahashi wants to know the number of adjacent participant pairs that result in an awkward handshake.
高桥想知道会导致尴尬握手的相邻参与者对数。

Specifically, for eachi = 1 , 2 , … , N − 1 i = 1, 2, \ldots, N-1i=1,2,,N1, check whether the color of participanti ii’s right-hand gloveR i R_iRiand the color of participanti + 1 i+1i+1’s left-hand gloveL i + 1 L_{i+1}Li+1are the same, and find the number of suchi ii.
具体而言,对于每个i = 1 , 2 , … , N − 1 i = 1, 2, …, N-1i=1,2,,N1,检查参与者i ii的右手手套颜色R i R_iRi与参与者i + 1 i+1i+1的左手手套颜色L i + 1 L_{i+1}Li+1是否相同,并统计满足条件的i ii的数量。

【输入】

N NN
L 1 L_1L1R 1 R_1R1
L 2 L_2L2R 2 R_2R2
⋮ \vdots
L N L_NLNR N R_NRN

  • The first line contains an integerN NNrepresenting the number of participants.
  • In the followingN NNlines, thei ii-th line( 1 ≤ i ≤ N ) (1 \leq i \leq N)(1iN)contains the characterL i L_iLirepresenting the color of participanti ii’s left-hand glove and the characterR i R_iRirepresenting the color of participanti ii’s right-hand glove, separated by a space. Each ofL i L_iLiandR i R_iRiis eitherN(navy) orS(white).

【输出】

Print the number of adjacent participant pairs that result in an awkward handshake, in a single line.

【输入样例】

4 N S S N N N S S

【输出样例】

2

【解题思路】

【算法标签】

#模拟#

【代码详解】

#include<bits/stdc++.h>usingnamespacestd;constintN=200005;intn,ans;// n: 物品数量,ans: 计数器charl[N],r[N];// l: 左颜色数组,r: 右颜色数组intmain(){cin>>n;// 读入物品数量for(inti=1;i<=n;i++){cin>>l[i]>>r[i];// 读入第i个物品的左颜色和右颜色}// 检查相邻两个物品的连接处颜色是否相同for(inti=1;i<n;i++){if(r[i]==l[i+1])// 如果前一个物品的右颜色等于后一个物品的左颜色{ans++;// 计数器加1}}cout<<ans<<endl;// 输出颜色相同的连接处数量return0;}

【运行结果】

4 N S S N N N S S 2
http://www.jsqmd.com/news/694796/

相关文章:

  • STM32 DAC输出波形实战避坑:为什么你的正弦波有毛刺?如何优化三角波线性度?
  • 维普AI率工具哪个好?2026年4月8款产品深度对比
  • DNSLog实战指南:三大主流平台特性解析与场景应用
  • 别再死记DH参数了!用MATLAB Robotic Toolbox快速验证你的机器人模型(附工作空间计算代码)
  • Linux下4G/5G模块实战:从AT指令到NetworkManager,手把手搞定蜂窝网络连接
  • 如何从已禁用 iTunes 连接的 iPhone 中恢复数据
  • 题解:AtCoder AT_awc0003_c Bargain Sale Selection
  • AI SoC全芯片DFT实战
  • 别再只用enable password了!思科设备密码安全进阶:配置加密的enable secret与Console口超时
  • 深度强化学习与自然语言理解的融合实践
  • 手写一个分布式RPC框架!
  • AirSim安装报错‘No module named numpy’?一个隐藏的依赖陷阱与解决方案
  • 面试官最爱问的C++服务器项目:TinyWebServer中Epoll与Reactor模式如何协同工作?
  • 如何在 Realme 上恢复已删除的联系人
  • 【电能质量扰动】基于ML和DWT的电能质量扰动分类方法研究(Matlab实现)
  • 从零到一:手写笔迹还原算法(InkCanvas)的深度剖析与实战应用
  • Pycharm里用Conda环境跑Selenium总报错?这份避坑指南帮你一次搞定所有依赖和路径问题
  • ArcGIS新手必看:别再搞混OBJECTID、FID和OID了,一次讲清区别和实战用法
  • NLP实战入门——从零构建智能对话系统(一)
  • 芯片设计中的“普通话”和“方言”:LEF/DEF文件在物理实现中的角色与避坑指南
  • 告别盲调!用瑞萨RA_FSP的ADC监测MCU内部温度与电压,手把手搭建系统健康检查
  • 华为防火墙模拟器(eNSP)从零搭建实验环境:手把手配置管理口并开启Web登录
  • 题解:AtCoder AT_awc0003_d Consecutive Practice Days
  • NCMDump终极解密指南:3分钟解锁网易云音乐NCM加密格式
  • ArcGIS Pro连接Excel受阻?一文详解Microsoft驱动安装与静默部署
  • 从手机APP反推ESP32-C3蓝牙开发:看懂这些GATT数据,你就能改任何例程
  • Silvaco Athena实战:从零搭建一个0.8微米NMOS管,手把手教你调阈值电压和提取关键参数
  • 别再只复制Key了!高德地图Geocoder.getLocation本地调用完整避坑指南
  • YOLOv5训练避坑指南:batch-size设为8的倍数真的更快?聊聊数据对齐与显存‘浪费’的那些事
  • 【电液伺服执行器与PI控制器】带有PI控制器的电液伺服执行器的模拟研究(Simulink仿真实现)