AC:1/3,score=1.4

https://ac.nowcoder.com/acm/contest/6915
第一题10分钟秒了,第二题知道怎么做写不出代码,第三题感觉代码写对了思路也对了,只能过40%,原来是递归受限,可惜了,但是好歹也小幅提升。
image.png

第一题:

题目:

image.png

答案:

  1. class Solution:
  2. def numbers(self , a , b , c , d , p ):
  3. # write code here
  4. x1=0
  5. y1=0
  6. def getp(a,b,p):
  7. k=a%p
  8. if k==0:temp=a
  9. else:temp = a+p-k
  10. if temp>b:return 0
  11. return (b-temp)//p+1
  12. x1=getp(a,b,p)
  13. y1=getp(c,d,p)
  14. x2=b-a+1-x1
  15. y2=d-c+1-y1
  16. return x1*y2+x1*y1+y1*x2

思路:

答案是xp数ynp数+xp数yp数+yp数*xnp数
主要数两个区间的p数有多少就可以了b``// p - (a-1) // p。写的稍微复杂一点也无伤大雅。

第二题:

题目:

image.png

答案:

  1. # class Point:
  2. # def __init__(self, a=0, b=0):
  3. # self.x = a
  4. # self.y = b
  5. #
  6. # 牛牛经过的房间数。
  7. # @param n int整型
  8. # @param x int整型
  9. # @param Edge Point类一维数组
  10. # @return int整型
  11. #
  12. from collections import defaultdict
  13. class Solution:
  14. def solve(self , n , x , Edge ):
  15. if x==1:return 0
  16. graph=defaultdict(list)
  17. for node in Edge:
  18. graph[node.x].append(node.y)
  19. graph[node.y].append(node.x)
  20. dx=[0]*(n+1)
  21. stack=[(-1,x,1)]# pre,cur,d
  22. while stack:
  23. pre,cur,d= stack.pop()
  24. dx[cur]=d
  25. for node in graph[cur]:
  26. if node!=pre:
  27. stack.append((cur,node,d+1))
  28. ans=0
  29. stack=[(-1,1,1)]
  30. while stack:
  31. pre,cur,d=stack.pop()
  32. if d>dx[cur] and d>ans:
  33. ans=d
  34. for node in graph[cur]:
  35. if node!=pre:
  36. stack.append((cur,node,d+1))
  37. return ans

思路:

x==1特判,考试时提醒过的。
评论区说dfs 可能会爆,你看我上面和下面都是dfs,事实上不会爆,是因为递归会爆,要import sys设置一下,下面的代码是dfs递归的。
第一个搜索,找出距离x的距离
第二个搜索,找出距离1的距离
答案更新:距离1的大于距离x的,就可以更新了,注意第一个房间也算,所以起点为1.

  1. import sys
  2. sys.setrecursionlimit(10000000)
  3. class Solution:
  4. def solve(self , n , x , Edge ):
  5. # write code here
  6. from collections import defaultdict
  7. if x == 1:
  8. return 0
  9. graph = defaultdict(list)
  10. for i in Edge:
  11. graph[i.x].append(i.y)
  12. graph[i.y].append(i.x)
  13. dx = [0]*(n+1)
  14. def dfx(pre, cur, d):
  15. dx[cur] = d
  16. for q in graph[cur]:
  17. if q != pre:
  18. dfx(cur, q, d+1)
  19. dfx(-1, x, 1)
  20. result = [0]
  21. def dfs(pre, cur, d):
  22. if d > dx[cur]:
  23. if d > result[0]:
  24. result[0] = d
  25. for q in graph[cur]:
  26. if q != pre:
  27. dfs(cur, q, d+1)
  28. dfs(-1, 1, 1)
  29. return result[0]

第三题:

题目:

image.png

答案:

  1. import sys
  2. sys.setrecursionlimit(10000000)
  3. class Solution:
  4. def arrange(self , firstRow , secondRow ):
  5. n=len(firstRow)
  6. def dfs(i,count):
  7. if i==n:
  8. self.ans=min(self.ans,count)
  9. return
  10. if i==0:
  11. dfs(i+1,count)
  12. firstRow[i],secondRow[i]=secondRow[i],firstRow[i]
  13. dfs(i+1,count+1)
  14. firstRow[i],secondRow[i]=secondRow[i],firstRow[i]
  15. else:
  16. if firstRow[i]<firstRow[i-1] and secondRow[i]>secondRow[i-1]:
  17. dfs(i+1,count)
  18. if firstRow[i]>secondRow[i-1] and secondRow[i]<firstRow[i-1]:
  19. firstRow[i],secondRow[i]=secondRow[i],firstRow[i]
  20. dfs(i+1,count+1)
  21. firstRow[i],secondRow[i]=secondRow[i],firstRow[i]
  22. return
  23. self.ans=float("inf")
  24. dfs(0,0)
  25. if self.ans==float("inf"):return -1
  26. return self.ans

思路:

我的思路是对的,只不过在牛客网递归有限制,要import sys 设置一下,我的本质还是dp。