欢迎大家订阅我的专栏:算法题解:C++与Python实现!
本专栏旨在帮助大家从基础到进阶 ,逐步提升编程能力,助力信息学竞赛备战!
专栏特色
1.经典算法练习:根据信息学竞赛大纲,精心挑选经典算法题目,提供清晰的代码实现与详细指导,帮助您夯实算法基础。
2.系统化学习路径:按照算法类别和难度分级,从基础到进阶,循序渐进,帮助您全面提升编程能力与算法思维。
适合人群:
- 准备参加蓝桥杯、GESP、CSP-J、CSP-S等信息学竞赛的学生
- 希望系统学习C++/Python编程的初学者
- 想要提升算法与编程能力的编程爱好者
附上汇总帖:AtCoder Beginner Contest竞赛题解 | 汇总
【题目来源】
洛谷:[AT_abc437_b ABC437B] Tombola - 洛谷
【题目描述】
There is a grid withH HHrows andW WWcolumns. Each square has one integer written on it, and these integers are distinct. The square at thei ii-th row from the top andj jj-th column from the left has the integerA i , j A_{i,j}Ai,jwritten on it.
有一个H HH行W WW列的网格。每个方格上写有一个整数,且这些整数互不相同。位于从上往下第i ii行、从左往右第j jj列的方格上写有整数A i , j A_{i,j}Ai,j。
Now, the host called outN NNdistinct integersB 1 , … , B N B_1, \dots, B_NB1,…,BN.
现在,主持人报出了N NN个互不相同的整数B 1 , … , B N B_1, …, B_NB1,…,BN。
If you find, for each row, how many of the integers called out by the host are contained in that row, what is the maximum value among these?
若针对每一行,统计该行中包含的主持人所报出的整数个数,则这些个数中的最大值是多少?
【输入】
The input is given from Standard Input in the following format:
H HHW WWN NNA 1 , 1 A_{1,1}A1,1⋯ \cdots⋯A 1 , W A_{1,W}A1,W⋮ \vdots⋮A H , 1 A_{H,1}AH,1⋯ \cdots⋯A H , W A_{H,W}AH,WB 1 B_1B1⋮ \vdots⋮B N B_NBN
【输出】
Output the answer in one line.
【输入样例】
3 4 5 12 3 5 7 6 10 11 9 1 2 4 8 2 4 9 6 11【输出样例】
3【代码详解】
#include<bits/stdc++.h>usingnamespacestd;constintN=95;// 最大矩阵大小inth,w,n;// h: 行数, w: 列数, n: 数字列表长度inta[N][N];// h×w的矩阵intb[N];// 包含n个数字的列表intmain(){// 输入矩阵的行数、列数和数字列表长度cin>>h>>w>>n;// 输入矩阵元素for(inti=1;i<=h;i++){for(intj=1;j<=w;j++){cin>>a[i][j];}}// 输入数字列表for(inti=1;i<=n;i++){cin>>b[i];}intmaxn=-1e9;// 初始化最大计数为极小值// 遍历每一行for(inti=1;i<=h;i++){intcnt=0;// 当前行的匹配计数// 遍历当前行的每个元素for(intj=1;j<=w;j++){// 遍历数字列表中的每个数字for(intk=1;k<=n;k++){// 如果矩阵元素等于列表中的数字if(b[k]==a[i][j]){cnt++;// 增加计数}}}// 更新最大计数maxn=max(maxn,cnt);}// 输出最大计数cout<<maxn<<endl;return0;}【运行结果】
3 4 5 12 3 5 7 6 10 11 9 1 2 4 8 2 4 9 6 11 3