Variational design of finite-difference schemes for initial value problems with an integral invariant