I guess it is like this! Actually, it is the same with peking2's code, and i
just try to explain it.
assume
sum(1+2+...,+ n-1) < x
sum(1+2+...,+n) > x
Construction (greedy) the moving sequence as following:
[-k1] means left movement with size k1.
1, 2, 3, 4, ..., [-k1], k1+1,...,[-k2], k2+1,..., kn-1, -[kn], km+1, ... n +
km
x = sum(1+2...+n+km) - 2(k1+k2+k3+...)
k1+k2+k3+...+km = [sum(1+2+...+n+km) - x]/2.
min m = 1 or 2, depends on the value of x.