【发布时间】:2021-11-13 06:03:04
【问题描述】:
我正在为即将在 12 月举行的 USACO 比赛进行练习,并试图解决这个问题:http://www.usaco.org/index.php?page=viewproblem2&cpid=1131
出于对计算机科学的热爱以及有一天成为“Bessie 博士”的诱惑,母牛 Bessie 参加了计算机科学博士课程。经过一段时间的学术研究,她现已发表了N篇论文(1≤N≤100000),她的第i篇论文在研究文献中积累了其他论文的ci引用(0≤ci≤100000)。 Bessie 听说一个学者的成功可以通过他们的 h 指数来衡量。 h-index 是最大的数 h,使得研究人员至少有 h 篇论文,每篇论文至少有 h 次引用。例如,有 4 篇论文和各自的引用计数 (1,100,2,3) 的研究人员的 h-index 为 2,而如果引用计数为 (1,100,3,3),则 h-index 将为 3。
为了提高她的 H 指数,Bessie 计划撰写一篇调查文章,引用她过去的几篇论文。由于页数限制,她在本次调查中最多可以包含 L 次引用(0≤L≤100000),当然她每篇论文最多可以引用一次。
帮助 Bessie 确定她在完成这份调查后可能达到的最大 h 指数。
请注意,Bessie 的研究顾问可能应该在某个时候告诉她,仅仅为了提高一个人的 h 指数而编写一份调查在道德上是有问题的;此处不建议其他学者效仿 Bessie 的做法。
输入格式(输入来自终端/标准输入):
输入的第一行包含 N 和 L。 第二行包含 N 个空格分隔的整数 c1,…,cN。
输出格式(打印输出到终端/标准输出):
Bessie 在完成调查后可能达到的最大 h 指数。
样本输入:
4 0
1 100 2 3
样本输出:
2
Bessie 不能引用她过去的任何论文。如上所述,(1,100,2,3) 的 h-index 为 2。
样本输入:
4 1
1 100 2 3
样本输出:
3
如果 Bessie 引用了她的第三篇论文,则引用计数变为 (1,100,3,3)。如上所述,这些计数的 h-index 为 3。
评分:
- 测试用例 1-7 满足 N≤100。
- 测试用例 8-10 满足 N≤1000。
- 测试用例 11-17 满足 N≤100000。
我的 Python 3 代码解决这个问题:
# read data and sort list
line = input().strip().split()
n, l = int(line[0]), int(line[1])
cite = [int(i) for i in input().strip().split()]
cite.sort()
# initiate the integer that will keep track of the largest number that satisfies the conditions and the set we will use to check whether or not a number has already been checked
max_num = 0
nums = set()
# iterate over every number
for i in range(n):
if cite[i] in nums:
# skip the iteration if the number has already been checked
continue
# add the number to the set
nums.add(cite[i])
# if any numbers are 1 less than the current number, keep track of them to add later on in the program. set extra to l if there are more numbers that are less than the current number that citations to be used.
if cite.count(cite[i]-1) <= l:
extra = cite.count(cite[i]-1)
else:
extra = l
# check if the h-index is greater than the currently largest h-index and replace it if it is.
if len(cite[i:]) + extra >= cite[i] and cite[i] >= max_num:
max_num = cite[i]
# for a list of citations that only have 1 integer in it, check if l is > 0. if it is, the max number is that number + 1.
if n == 1 and l > 0:
max_num = cite[0] + 1
print(max_num)
我相信逻辑是正确的,但是当输入是
1000 251
500 500 500 500 500 502 500 502 500 502 500 500 500 500 500 500 500 500 502 502 500 500 500 500 500 500 500 500 500 500 500 500 502 500 500 500 500 502 500 500 500 500 500 500 500 500 500 500 500 500 500 502 500 502 500 500 500 500 500 502 500 500 500 500 500 502 502 500 502 500 500 500 500 502 502 502 500 500 500 500 500 502 502 502 500 500 500 500 502 500 500 500 500 500 500 500 500 502 500 500 500 500 500 500 500 500 500 500 502 500 500 502 500 500 500 500 500 500 500 500 502 502 500 500 502 500 500 500 502 500 500 502 502 500 500 500 502 500 500 502 500 500 500 502 502 500 500 500 500 500 500 502 502 500 500 500 500 500 500 500 500 500 500 500 500 500 500 502 500 500 500 500 500 502 502 500 500 500 500 500 502 502 502 500 500 502 500 500 500 500 500 500 500 500 502 500 500 500 500 500 502 502 500 502 500 502 500 500 500 500 500 502 502 500 500 502 500 502 502 500 500 502 500 500 502 500 500 502 500 500 502 500 500 502 500 500 500 500 500 500 500 500 502 500 502 502 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 502 500 500 500 500 502 502 502 500 500 500 502 500 500 500 500 502 500 500 500 502 502 502 502 500 500 500 500 500 500 500 500 500 500 502 500 502 500 502 502 500 502 500 500 500 500 500 502 500 500 500 500 500 502 500 500 500 500 502 500 500 500 500 500 500 500 500 500 500 500 502 500 500 500 500 502 500 500 500 500 502 502 500 502 502 500 500 500 502 502 500 500 500 502 502 500 500 500 500 500 500 500 500 500 500 500 500 502 502 500 502 502 500 500 500 500 500 502 500 502 500 500 500 500 500 502 500 500 500 500 500 502 502 502 500 500 500 500 500 500 502 502 500 500 500 502 500 500 502 500 500 502 500 500 500 502 502 500 502 500 500 500 500 500 500 500 500 500 500 500 500 500 502 502 500 502 500 502 502 500 500 500 500 500 500 500 500 502 500 502 500 500 502 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 502 500 502 502 500 500 500 500 502 500 500 502 502 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 502 502 500 502 500 502 502 500 500 502 500 502 502 500 502 500 500 500 500 500 502 500 502 500 500 500 500 500 500 500 500 500 502 500 500 500 500 500 502 502 502 502 502 500 500 502 500 502 500 500 502 500 500 500 500 500 500 502 502 500 500 502 500 500 500 500 500 500 500 500 500 500 500 502 500 500 502 500 500 502 500 500 500 500 500 502 500 500 500 500 500 500 502 500 502 500 500 500 500 500 500 502 502 500 502 502 500 502 500 500 500 500 500 500 500 502 500 502 500 502 502 500 500 500 500 500 500 502 502 502 500 500 500 500 500 500 500 502 502 500 500 500 500 500 500 500 500 500 500 500 500 500 502 500 502 500 500 502 502 502 500 500 500 500 500 502 500 502 500 500 500 500 500 500 500 500 500 500 500 500 500 502 500 500 500 502 500 502 500 502 502 500 500 500 500 500 500 502 502 500 500 502 500 500 500 500 500 500 500 500 500 502 500 500 500 502 502 500 502 500 502 502 500 500 500 500 502 502 502 502 500 500 500 502 500 500 500 500 500 500 500 500 502 500 502 500 500 502 500 502 500 500 500 500 500 502 500 500 500 500 502 500 500 502 500 502 502 500 502 500 500 500 500 500 500 500 502 500 500 502 500 500 502 500 500 500 500 500 502 502 500 500 500 500 500 500 500 502 500 500 500 500 500 502 502 500 500 502 502 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 500 502 500 502 502 500 500 500 500 502 500 500 500 502 500 500 500 500 500 500 500 500 500 500 500 500 502 500 500 500 500 500 500 500 502 500 500 500 502 500 500 500 500 500 500 500 500 500 500 502 500 500 500 500 500 500 500 502 502 502 502 500 500 500 500 502 500 502 500 500 500 500 500 500 502 502 500 502 500 500 500 502 500 500 500 500 500 500 500 500 500 502 500 500 500 500 500 500 500 500 502 500 500 502 502 500 500 502 500 500 500 502 502 500 500 500 502 500 502 500 500 502 500 500 500 502 500 500 500 500 500 500 502 500 500 500 500 502 502 500 502 500 500 502 500 500 500 500 500 500 500 502 500 502 500 502 500 500 502 500 500 500 500 502 502 500 500
它返回错误的答案。它打印500,但应该是501
我的实现是否有错误,还是我的逻辑有问题?
注意:我也遇到了时间复杂度的问题,例如当我遇到 n = 100000 和 l = 50000 的问题时。
【问题讨论】:
-
当您说“它返回错误”时,您的意思是运行时错误吗?应该有什么输出?
-
只是一种预感-我认为您的问题可能在于假设每篇论文的引用次数都是唯一的(通过使用
set()) -
@kcsquared 不,我的意思是错误的答案。我会改变的
-
@DillonDavis 该集合只是为了跟踪我已经迭代过的数字
标签: python algorithm optimization logic