Advertisement
Guest User

Untitled

a guest
Apr 18th, 2021
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.87 KB | None | 0 0
  1. # Solution for Google Kick Start 2021 round B problem B
  2.  
  3. def solve(a)
  4.   # length of arithmetic array ending at i-th element (with 0 substitutions)
  5.   l0 = [0] * a.size
  6.   # length of arithmetic array ending at i-th element (with 1 substitution)
  7.   l1 = [0] * a.size
  8.  
  9.   l0[0] = l1[0] = 1
  10.   l0[1] = l1[1] = 2
  11.  
  12.   2.upto(a.size - 1) do |i|
  13.     step0 = a[i - 1] - a[i - 2]
  14.  
  15.     if a[i] - a[i - 1] == step0
  16.       l0[i] = l0[i - 1] + 1
  17.     else
  18.       l0[i] = 2
  19.     end
  20.  
  21.     step1 = a[i - l1[i - 1] + 1] - a[i - l1[i - 1]]
  22.  
  23.     if a[i] == a[i - l1[i - 1]] + step1 * l1[i - 1]
  24.       l1[i] = l1[i - 1] + 1
  25.     else
  26.       l1[i] = l0[i - 1] + 1
  27.     end
  28.   end
  29.  
  30.   return l1.max
  31. end
  32.  
  33. t = gets.to_s.to_i
  34. 1.upto(t) do |casenum|
  35.   n = gets.to_s.to_i
  36.   a = gets.to_s.strip.split.map {|x| x.to_i }
  37.  
  38.   printf("Case #%d: %s\n", casenum, [solve(a), solve(a.reverse)].max)
  39. end
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement