UVa Problem Solution: 10152 - ShellSort


Let the required stack to be the sorted sequence, and the original stack to be the sequence to sort. A turtle is numbered by its position in the required stack: the top is 0, the second is 1, and so on. A turtle is said in reverse order if its number is less than the biggest number of the turtles above it in. Each time, find the turtle with the maximum number of all the turtles in reversed order, and let it climb up to the top.

In fact, all the turtles that have ever been in reverse order need to crawl out of the stack. Choose turtle with the maximum number would keep it from crawling out again.

Code:
  1. /*************************************************************************
  2.  * Copyright (C) 2008 by liukaipeng                                      *
  3.  * liukaipeng at gmail dot com                                           *
  4.  *************************************************************************/
  5. /* @JUDGE_ID 00000 10152 C++ "ShellSort" */
  6. #include <algorithm>
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <deque>
  10. #include <fstream>
  11. #include <iostream>
  12. #include <list>
  13. #include <map>
  14. #include <queue>
  15. #include <set>
  16. #include <stack>
  17. #include <string>
  18. #include <vector>
  19. using namespace std;
  20.      
  21. int const namesize = 81;
  22. int const turtlecount = 200;
  23.           
  24. struct strcomp
  25. {
  26.   bool operator()(char const *s1, char const *s2)
  27.   { return strcmp(s1, s2) < 0; }
  28. };
  29. int sort_turtles(int *turtles, int nturtles, int moves[])
  30. {
  31.   int nmoves = 0;
  32.   while (true) {
  33.     int curm = -1;
  34.     int revm = -1;
  35.     int move = -1;
  36.     for (int i = 0; i < nturtles; ++i) {
  37.       if (turtles[i] > curm) {
  38.         curm = turtles[i];
  39.       } else if (turtles[i] < curm && turtles[i] > revm) {
  40.         revm = turtles[i];
  41.         move = i;
  42.       }
  43.     }
  44.     if (revm < 0) break;
  45.     for (int i = move; i > 0; --i) turtles[i] = turtles[i-1];
  46.     turtles[0] = revm;
  47.     moves[nmoves++] = revm;
  48.   }
  49.   return nmoves;
  50. }
  51. int main(int argc, char *argv[])
  52. {
  53. #ifndef ONLINE_JUDGE
  54.   freopen((string(argv[0]) + ".in").c_str(), "r", stdin);
  55.   freopen((string(argv[0]) + ".out").c_str(), "w", stdout);
  56. #endif
  57.   int ncases;
  58.   cin >> ncases;
  59.   while (ncases-- > 0) {
  60.     int nturtles;
  61.     cin >> nturtles;
  62.     cin.ignore(2048, '/n');
  63.     char origin[turtlecount][namesize];
  64.     for (int i = 0; i < nturtles; ++i) {
  65.       cin.getline(origin[i], namesize);
  66.     }
  67.     char names[turtlecount][namesize];
  68.     map<char *, int, strcomp> nameids;
  69.     for (int i = 0; i < nturtles; ++i) {
  70.       cin.getline(names[i], namesize);
  71.       nameids.insert(make_pair(names[i], i));
  72.     }
  73.     int turtles[turtlecount];
  74.     for (int i = 0; i < nturtles; ++i) {
  75.       turtles[i] = nameids[origin[i]];
  76.     }
  77.     int moves[turtlecount];
  78.     int nmoves = sort_turtles(turtles, nturtles, moves);
  79.     for (int i = 0; i < nmoves; ++i) {
  80.       cout << names[moves[i]] << '/n';
  81.     }
  82.     cout << '/n';
  83.   }
  84.   return 0;
  85. }

發佈了69 篇原創文章 · 獲贊 2 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章