USACO Livestock Lineup in Java?

I haven’t been able to solve livestock lineup for the 2019 December Bronze contest.

Could anyone help?

What have you tried? Have you looked at the editorial yet? If so, what part of the editorial do you not understand?

Hello. I understand the problem but I could not figure out how to solve it. I looked at the editorial and it is in C++ (I use Java). I can understand most C++ code for Bronze problems but there was a function next_permutation that looks like it plays a big role in the solution. I couldn’t find any solutions in Java.

Thanks for the reply,


You basically want to generate every permutation (every possible ordering) of cows, then find the first such permutation that is “valid” (ie satisfies all the given constraints).

next_permutation is a C++ method that takes an array and permutes it; it can be used to loop through all the permutations of an array. I don’t believe there is a Java equivalent, but you can code one yourself with recursion (Google it, or modify some of the code here: