On Sat, May 14, 2011 at 2:35 PM, shubham <[email protected]> wrote:
> thanks for the reply... > so the problem is with the algorithm chosen. > can you suggest me some links where to for > some good algorithms. actually this is the first > time i am participating in any contest & also > i am a beginner in the programming. Look at the description of the contest: http://code.google.com/codejam/contest/dashboard?c=975485#s=a&a=2 It not necessary to do anything -- you can generate the best solution instantly: - Patrick will be happy if bitwise "xor"-combination of all candies is zero. - then give Patrick the smalles candy, and he is happy no need for a complex algorithm -- as soon as you realized this trivial solution... (see the link for a precise explication) In C++, you could do the following: - to do the xor-combination (untested): define a "xor"-functor analogous to plus, minus, ... from the standard library and then use "accumulate" to calculate the xor-operation template <class _Tp> struct do_xor : public std::binary_function<_Tp, _Tp, _Tp> { _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x xor __y; } }; if the result is 0, Patrick is happy, no matter what he gets. - give the smallest element to patrick and sum the remainder: if(std::accumulate(total.begin(), total.end(), 0, do_xor<int>() ) ==0) val = 0; else val = std::accumulate(total.begin()+1, total.end(), 0, plus<int>() ); (that works only if total has at least 1 element!) HTH, Axel -- You received this message because you are subscribed to the Google Groups "google-codejam" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-code?hl=en.
