fork download
  1. def f(k)
  2. gets
  3. as=gets.split(" ").map{|e| e.to_i+k}.sort
  4. as2=[]
  5. n=as[0]
  6. c=0
  7. as.each{|e|
  8. if e==n then
  9. c+=1
  10. else
  11. as2<<[n,c]
  12. c=1
  13. n=e
  14. end
  15. }
  16. as2<<[n,c]
  17. return as2
  18. end
  19. k=gets.to_i
  20. as1=f(k)
  21. bs1=f(0)
  22. p1=0
  23. p2=0
  24. ans=0
  25. while p1<as1.size && p2<bs1.size
  26. t1=as1[p1][0]
  27. t2=bs1[p2][0]
  28. if t1==t2 then
  29. ans+=as1[p1][1]*bs1[p2][1]
  30. p1+=1
  31. p2+=1
  32. elsif t1<t2 then
  33. p1+=1
  34. else
  35. p2+=1
  36. end
  37. end
  38. puts ans
Success #stdin #stdout 0.01s 8080KB
stdin
1
4
1 8 6 8
3
7 9 4
stdout
3