// Shellsort (shell sorting) // PC/Ultraviolet IDs: 110407/10152, popularity: B, success rate: average level: 2 // verdict: accepted // submission date: 2011-05-27 // UV Run Time: 0.656 S // copyright (c) 2011, Qiu. Metaphysis # Yeah dot net /// how can I move the SDK to achieve the minimum steps? It took me some time to think about this issue. Consider the following initial status and final status (for ease of interpretation, the serial number is marked with square brackets before the turtle name): // initial status: final status: // [4] Yertle [1] Oscar // [1] Oscar [2] BARON // [2] BARON [3] Lord // [3] Lord [4] Yertle // [5] King [5] King // [7] White [6] Kong // [6] Kong [7] White /// you can follow these six steps: target, six steps are the minimum steps required. /// [4] Yertle [6] Kong [5] King // [1] Oscar [4] Yertle [6] Kong // [2] BARON [1] Oscar >>>> [4] Yertle // [3] Lord ==>> [2] BARON ==>> [1] Oscar ==>// [5] King [3] Lord [2] BARON // [7] White >>>> [5] King [3] Lord // >>>> [6] Kong [7] White [7] White /// [4] Yertle [3] Lord [2] BARON // [5] King [4] Yertle [3] Lord // [6] Kong [5] King [4] Yertle // [1] Oscar ==>> [6] Kong ==>> [5] King ==>>// [2] Baron [1] Oscar [6] Kong // >>> [3] Lord>> [2] BARON >>> [1] Oscar // [7] White [7] White [7] White /// // [1] Oscar // [2] BARON // [3] Lord // [4] Yertle // [5] King // [6] Kong // [7] White /// the algorithm is as follows:: assume there are n turtles numbered 1 ~ N. For turtles numbered N, if the turtles numbered n-1 are under the turtles numbered n-1, place the turtles numbered n-1 at the top, then ,..., // The turtle of 2, 1 continues the above operation until the sorting is complete. # Include <iostream> using namespace STD; # define maxsize 200 # ifndef debug_mode // # define debug_mode # endifstruct turtle {string name; int index ;}; void shell_sort (Turtle start [], turtle last [], int capacity) {// assign the serial number to the initial state turtle. For (INT I = 0; I <capacity; I ++) for (Int J = 0; j <capacity; j ++) if (start [J]. name = last [I]. name) {start [J]. index = last [I]. index; break;} // sorts the initial turtle status. For (INT I = capacity; I> 1; I --) {// locate the two turtles numbered I and (I-1) in the array. Int current, previous; For (Int J = 0; j <capacity; j ++) {If (start [J]. index = I) Current = J; If (start [J]. index = (I-1) Previous = J;} // If the turtle whose serial number is (I-1) is under the turtle whose serial number is I, put it to the top. If (previous> current) {cout <start [previous]. name <Endl; Turtle TMP = start [previous]; for (Int J = previous; j> 0; j --) {start [J]. name = start [J-1]. name; start [J]. index = start [J-1]. index;} start [0]. name = TMP. name; start [0]. index = TMP. index ;}# ifdef debug_modecout <"<debug begin>" <Endl; For (Int J = 0; j <capacity; j ++) cout <start [J]. index <"" <start [J]. name <Endl; cout <"<Debu G end> "<Endl; # endif} int main (int ac, char * AV []) {INT cases, capacity; Turtle start [maxsize]; turtle last [maxsize]; CIN> cases; while (cases --) {CIN> capacity; cin. ignore (); // read the initial status. For (INT I = 0; I <capacity; I ++) Getline (CIN, start [I]. Name); // read the final state and assign the serial number to each turtle. For (INT I = 0; I <capacity; I ++) {Getline (CIN, last [I]. name); last [I]. index = (I + 1);} // start the shell sorting! Shell_sort (START, last, capacity); cout <Endl;} return 0 ;}