news 2026/6/20 2:20:51

题解:洛谷 AT_abc461_b [ABC461B] The Honest Woodcutters

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
题解:洛谷 AT_abc461_b [ABC461B] The Honest Woodcutters

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

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

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


【题目来源】

洛谷:AT_abc461_b [ABC461B] The Honest Woodcutters - 洛谷

【题目描述】

N NNwoodcutters1 , 2 , … , N 1, 2, \dots, N1,2,,Neach have one axe. All of them dropped their axes into a pond.
N NNaxes1 , 2 , … , N 1, 2, \dots, N1,2,,Nwere found sunk in the pond.
Each woodcutteri iiclaims that “I owned axeA i A_iAi.”
On the other hand, the goddess of this pond knows that the woodcutter who owned axei iiis woodcutterB i B_iBi.

Determine whether allN NNwoodcutters are telling the truth.

N NN个樵夫1 , 2 , … , N 1, 2, \dots, N1,2,,N各有一把斧头。他们都把斧头掉进了池塘里。
N NN把斧头1 , 2 , … , N 1, 2, \dots, N1,2,,N被发现沉在池塘中。
每个樵夫i ii声称:“我拥有斧头A i A_iAi。”
另一方面,池塘女神知道拥有斧头i ii的樵夫是樵夫B i B_iBi

判断所有N NN个樵夫是否都在说真话。

【输入】

The input is given from Standard Input in the following format:

N NN
A 1 A_1A1A 2 A_2A2… \dotsA N A_NAN
B 1 B_1B1B 2 B_2B2… \dotsB N B_NBN

【输出】

OutputYesif allN NNwoodcutters are telling the truth, andNootherwise.

【输入样例】

3 3 1 2 2 3 1

【输出样例】

Yes

【算法标签】

#入门

【代码详解】

#include<bits/stdc++.h>usingnamespacestd;constintN=105;// 定义最大数量intn;// 元素数量inta[N],b[N];// 位置数组a,比较数组bintmain()// 主函数{cin>>n;// 输入元素数量for(inti=1;i<=n;i++)// 读取第一组数据{intx;cin>>x;// 输入数字a[x]=i;// 记录数字x在数组a中的位置}for(inti=1;i<=n;i++)// 读取第二组数据cin>>b[i];// 输入第二组数字for(inti=1;i<=n;i++)// 比较两个数组if(a[i]!=b[i])// 如果位置不匹配{cout<<"No"<<endl;// 输出Noreturn0;// 结束程序}cout<<"Yes"<<endl;// 所有位置都匹配,输出Yesreturn0;// 程序正常结束}

【运行结果】

3 3 1 2 2 3 1 Yes
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/20 2:20:43

免费解锁iPhone激活锁的终极指南:使用applera1n让iOS设备重获新生

免费解锁iPhone激活锁的终极指南&#xff1a;使用applera1n让iOS设备重获新生 【免费下载链接】applera1n icloud bypass for ios 15-16 项目地址: https://gitcode.com/gh_mirrors/ap/applera1n 你是否曾经面对一台被激活锁锁住的iPhone&#xff0c;感觉它变成了昂贵的…

作者头像 李华
网站建设 2026/6/20 2:20:44

如何通过3个智能模块重塑你的Windows系统体验?

如何通过3个智能模块重塑你的Windows系统体验&#xff1f; 【免费下载链接】Win11Debloat A simple, lightweight PowerShell script that allows you to remove pre-installed apps, disable telemetry, as well as perform various other changes to declutter and customize…

作者头像 李华
网站建设 2026/6/17 18:39:32

SelfCheckGPT:零资源黑盒幻觉检测的架构级解决方案

SelfCheckGPT&#xff1a;零资源黑盒幻觉检测的架构级解决方案 【免费下载链接】selfcheckgpt SelfCheckGPT: Zero-Resource Black-Box Hallucination Detection for Generative Large Language Models 项目地址: https://gitcode.com/gh_mirrors/se/selfcheckgpt 大型语…

作者头像 李华
网站建设 2026/6/19 13:40:25

求职时间陷阱终结者:NewJob智能插件如何帮你避开80%的无效投递

求职时间陷阱终结者&#xff1a;NewJob智能插件如何帮你避开80%的无效投递 【免费下载链接】NewJob 一眼看出该职位最后修改时间&#xff0c;绿色为2周之内&#xff0c;暗橙色为1.5个月之内&#xff0c;红色为1.5个月以上 项目地址: https://gitcode.com/GitHub_Trending/ne/…

作者头像 李华
网站建设 2026/6/14 6:12:57

深度解析RookieAI_yolov8:基于YOLOv8的AI自瞄系统架构与实战指南

深度解析RookieAI_yolov8&#xff1a;基于YOLOv8的AI自瞄系统架构与实战指南 【免费下载链接】RookieAI_yolov8 基于yolov8实现的AI自瞄项目 AI self-aiming project based on yolov8 项目地址: https://gitcode.com/gh_mirrors/ro/RookieAI_yolov8 RookieAI_yolov8是一…

作者头像 李华