三亩地 三亩地SAN MU DI · CODE DIARY
ARTICLE DETAIL

日记详情

真实记录编程学习的某一天,欢迎挑你感兴趣的翻一翻。

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

题解:洛谷 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
← 返回列表