题解:洛谷 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 NNA 1 A_1A1A 2 A_2A2… \dots…A N A_NANB 1 B_1B1B 2 B_2B2… \dots…B N B_NBN【输出】OutputYesif allN NNwoodcutters are telling the truth, andNootherwise.【输入样例】3 3 1 2 2 3 1【输出样例】Yes【算法标签】#入门【代码详解】#includebits/stdc.husingnamespacestd;constintN105;// 定义最大数量intn;// 元素数量inta[N],b[N];// 位置数组a比较数组bintmain()// 主函数{cinn;// 输入元素数量for(inti1;in;i)// 读取第一组数据{intx;cinx;// 输入数字a[x]i;// 记录数字x在数组a中的位置}for(inti1;in;i)// 读取第二组数据cinb[i];// 输入第二组数字for(inti1;in;i)// 比较两个数组if(a[i]!b[i])// 如果位置不匹配{coutNoendl;// 输出Noreturn0;// 结束程序}coutYesendl;// 所有位置都匹配输出Yesreturn0;// 程序正常结束}【运行结果】3 3 1 2 2 3 1 Yes