Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Solution for Google Kick Start 2021 round B problem B
- def solve(a)
- # length of arithmetic array ending at i-th element (with 0 substitutions)
- l0 = [0] * a.size
- # length of arithmetic array ending at i-th element (with 1 substitution)
- l1 = [0] * a.size
- l0[0] = l1[0] = 1
- l0[1] = l1[1] = 2
- 2.upto(a.size - 1) do |i|
- step0 = a[i - 1] - a[i - 2]
- if a[i] - a[i - 1] == step0
- l0[i] = l0[i - 1] + 1
- else
- l0[i] = 2
- end
- step1 = a[i - l1[i - 1] + 1] - a[i - l1[i - 1]]
- if a[i] == a[i - l1[i - 1]] + step1 * l1[i - 1]
- l1[i] = l1[i - 1] + 1
- else
- l1[i] = l0[i - 1] + 1
- end
- end
- return l1.max
- end
- t = gets.to_s.to_i
- 1.upto(t) do |casenum|
- n = gets.to_s.to_i
- a = gets.to_s.strip.split.map {|x| x.to_i }
- printf("Case #%d: %s\n", casenum, [solve(a), solve(a.reverse)].max)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement