人数统计
小易的公司一共有n名员工, 第i个人每个月的薪酬是xi万元。
现在小易的老板向小易提了m次询问, 每次询问老板都会给出一个整数k, 小易要快速回答老板工资等于k的员工的数量。
输入描述:
第一行,两个空格间隔的整数n和m,表示人数和提问的次数
第二行,n个用空格间隔的整数xi,表示每名员工的薪酬
接下来有m行,每行一个整数,表示老板的一次提问。
1<=m<=80000,1<=n<=100000,1<=xi<=500,000,000
输出描述:
m行,每行一个整数,表示对应提问的答案
示例1
输入
7 4
6 2 1 2 6 2 5
6
5
8
2
输出
2
1
0
3
参考1:
n,m = map(int,input().split()) num = list(map(int,input().split())) for _ in range(m): k = int(input()) r = num.count(k) #计数 print(r)
您的代码已保存
运行超时:您的程序未能在规定时间内运行结束,请检查是否循环有错或算法复杂度过大。
case通过率为50.00%
参考2:
在python中简单使用count会导致超时,此处应生成dict,然后查找。
from collections import Counter n,m = map(int,input().split()) num = list(map(int,input().split())) count_dict = Counter(num) for _ in range(m): q = int(input().strip()) print(count_dict[q])
您的代码已保存
答案正确:恭喜!您提交的程序通过了所有的测试用例

浙公网安备 33010602011771号