Skip to content

Instantly share code, notes, and snippets.

@DNNX
Created May 14, 2011 13:46
Show Gist options
  • Save DNNX/972224 to your computer and use it in GitHub Desktop.
Save DNNX/972224 to your computer and use it in GitHub Desktop.
GCJ 2011 CandySplitting problem
class GCJSolution
def initialize(input = STDIN, output = STDOUT)
@input = input
@output = output
end
def read_strings
@input.gets.chomp.split(' ')
end
def read_ints
read_strings.map(&:to_i)
end
def read_int
@input.gets.to_i
end
def read_solve_write_all
number_of_tests = read_int
for test_no in 1..number_of_tests
test_case = parse_input()
ans = solve(test_case)
@output.puts "Case ##{test_no}: #{ans}"
end
number_of_tests
end
end
class CandySplitting < GCJSolution
def parse_input
read_int
read_ints
end
def solve(a)
xor = a.inject(0, &:^)
if xor == 0
sum = a.inject(0, &:+)
sum - a.min
else
'NO'
end
end
end
CandySplitting.new.read_solve_write_all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment