8.28 小红书 AC了两道
1.排队 排序
import sys
line1 = sys.stdin.readline()
n = int(line1.split()[0])
index = int(line1.split()[-1])
summ = []
for i in range(n):
line2 = sys.stdin.readline()
summ.append([i+1,sum(int(i)for i in line2.split())])
summ = sorted(summ,key= lambda x:x[1],reverse=True)
for i,item in enumerate(summ):
k,v = item[0],item[1]
if k == index:
print(i+1)
2.法术(二分查找)
import sys#Java笔试面试#
# line1 = sys.stdin.readline()
# line2 = sys.stdin.readline()
line1 = "3 3"
k = int(line1.split()[1])
line2 = "3 2 1"
arr = map(int, line2.split())
def get_right_index(arr, k):
i = 0
j = len(arr) - 1
while i < j:
mid = i + (j - i) // 2
if arr[mid] < k:
i = mid + 1
else:
j = mid
if arr[j] >= k:
return j
else:
return -1
arr = sorted(arr)
summ = 0
for item in arr:
div = k/item
index = get_right_index(arr,div)
if index != -1:
summ += (len(arr) - index)
for item in arr:
if item **2 >k:
summ -= 1
print(summ)