本来想通过一些笔试真题练一下输入输出 ,然后被深深的伤害了
meituan-002. 小美的仓库整理
美团的题 写了 个 n^2 超时 简直又臭又长
import heapq
n = int(input())
wit = [int(w) for w in input().strip().split() ]
num = [int(n) for n in input().strip().split() ]
lis = [0,]
for i in range(n):
lis.append(wit[i]+lis[-1])
#ed = [0,n]
heap =[]
#reslis =[(lis[-1],0,n+1)]
heapq.heappush(heap,(-lis[-1],0,n+1))
bef = []
for i in num :
wit[i-1] = 0
flag = 0
ald = []
sres,x,y = heapq.heappop(heap)
if x<i<y:
newre = lis[i-1]-lis[x]
newry = lis[y-1] -lis[i]
heapq.heappush(heap,(-newre,x,i))
heapq.heappush(heap,(-newry,i,y))
#ald.append()
else :
bef.append(i)
heapq.heappush(heap,(sres,x,y))
j = 0
while j <len(bef):
if heap:
sres,x,y = heapq.heappop(heap)
if x<bef[j]<y:
newre = lis[bef[j]-1]-lis[x]
newry = lis[y-1] -lis[bef[j]]
heapq.heappush(heap,(-newre,x,bef[j]))
heapq.heappush(heap,(-newry,bef[j],y))
ald.append(j)
del bef[j]
j=0
else :
j+=1
heapq.heappush(heap,(sres,x,y))
print(-heap[0][0])
逆向+前缀和+有序集合 TQL
import bisect
n = int(input())
wit = [int(w) for w in input().strip().split() ]
num = [int(n) for n in input().strip().split() ]
lis = [0,]
for i in range(n):
lis.append(wit[i]+lis[-1])
#ed = [0,n]
heap =[]
#reslis =[(lis[-1],0,n+1)]
bef = []
#----index的有序集合
idxs = [-1] + [x for x in range(n)] + [n]
#----结果数组
res = [0 for _ in range(n)]
res[n - 1] = 0
#----逆序
for i in range(n - 1, 0, -1):
idx = num[i]-1
ID = bisect.bisect_left(idxs, idx)
#print(idxs,ID,idx)
l = idxs[ID - 1]
r = idxs[ID + 1]
idxs.pop(ID)
cur = lis[r] - lis[l + 1]
res[i - 1] = max(res[i], cur)
for x in res:
print(x)
meituan-005. 小美的区域会议
这个题在leetcode 是困难题 但是感觉比上一题要简单 实际并不简单 只是比上一题
首先根据输入构建 邻接表 为了满足所有的方案数,可以选择以每个节点为出发点,同时以其为最低等级,然后深度优先搜索, 对其邻居进行搜索,若等级在要求范围内则继续搜索,同时可选阶乘最大值加1
这样就可以遍历完所有可能
n,k = map(int,input().split())
graph = [[] for _ in range(n)]
mod = 10 ** 9 + 7
## 构建邻接表
for i in range(n-1):
x,y = [int(num) for num in input().strip().split()]
graph[x-1].append(y-1)
graph[y-1].append(x-1)
lev = [int(num) for num in input().strip().split()]
#print(graph,lev)
def dfs(star,now,last):
res = 1
for node in graph[now]:
if node != last and lev[star]<=lev[node]<=lev[star]+k :
if lev[star] == lev[node] and node <star:
continue
res *=(dfs(star,node,now)+1)
res %= mod
return res
sumres =0
for i in range(n):
sumres+=dfs(i,i,-1)
sumres%=mod
print(sumres%mod)