Hints for SRM 351, D2, 1000-Pointer (InsertSort)

James S. Plank

Sun Nov 3 12:32:31 EST 2013
Problem Statement.
Here's the thought that helped me solve the problem. Look at the first number. If it's the smallest number, then that number will contribute zero to the solution. Otherwise, you have two options to have that number hit its final resting place:

This thought leads to a shortest path solution, where we build the graph as we process Dijkstra's algorithm. Each node will be labeled with a string that has theArray.size() elements. Initially it will be all dashs. We take the initial node and put it onto our multimap with a distance of zero. We then process nodes as in Dijkstra's algorithm. Here's how we process a node with string s and distance d.

Here are graphs for some of the examples:

Example 0


Example 1


Example 3