UVa Problem Solution: 10152 - ShellSort
来源:互联网 发布:java引号转义字符 编辑:程序博客网 时间:2024/06/07 07:22
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:
- /*************************************************************************
- * Copyright (C) 2008 by liukaipeng *
- * liukaipeng at gmail dot com *
- *************************************************************************/
- /* @JUDGE_ID 00000 10152 C++ "ShellSort" */
- #include <algorithm>
- #include <cstdio>
- #include <cstring>
- #include <deque>
- #include <fstream>
- #include <iostream>
- #include <list>
- #include <map>
- #include <queue>
- #include <set>
- #include <stack>
- #include <string>
- #include <vector>
- using namespace std;
- int const namesize = 81;
- int const turtlecount = 200;
- struct strcomp
- {
- bool operator()(char const *s1, char const *s2)
- { return strcmp(s1, s2) < 0; }
- };
- int sort_turtles(int *turtles, int nturtles, int moves[])
- {
- int nmoves = 0;
- while (true) {
- int curm = -1;
- int revm = -1;
- int move = -1;
- for (int i = 0; i < nturtles; ++i) {
- if (turtles[i] > curm) {
- curm = turtles[i];
- } else if (turtles[i] < curm && turtles[i] > revm) {
- revm = turtles[i];
- move = i;
- }
- }
- if (revm < 0) break;
- for (int i = move; i > 0; --i) turtles[i] = turtles[i-1];
- turtles[0] = revm;
- moves[nmoves++] = revm;
- }
- return nmoves;
- }
- int main(int argc, char *argv[])
- {
- #ifndef ONLINE_JUDGE
- freopen((string(argv[0]) + ".in").c_str(), "r", stdin);
- freopen((string(argv[0]) + ".out").c_str(), "w", stdout);
- #endif
- int ncases;
- cin >> ncases;
- while (ncases-- > 0) {
- int nturtles;
- cin >> nturtles;
- cin.ignore(2048, '/n');
- char origin[turtlecount][namesize];
- for (int i = 0; i < nturtles; ++i) {
- cin.getline(origin[i], namesize);
- }
- char names[turtlecount][namesize];
- map<char *, int, strcomp> nameids;
- for (int i = 0; i < nturtles; ++i) {
- cin.getline(names[i], namesize);
- nameids.insert(make_pair(names[i], i));
- }
- int turtles[turtlecount];
- for (int i = 0; i < nturtles; ++i) {
- turtles[i] = nameids[origin[i]];
- }
- int moves[turtlecount];
- int nmoves = sort_turtles(turtles, nturtles, moves);
- for (int i = 0; i < nmoves; ++i) {
- cout << names[moves[i]] << '/n';
- }
- cout << '/n';
- }
- return 0;
- }
- UVa Problem Solution: 10152 - ShellSort
- UVa Problem 10152 ShellSort (龟壳排序)
- UVa Problem 10152 ShellSort (龟壳排序)java实现
- uva 10152 shellsort
- Uva 10152 ShellSort
- uva 10152 - ShellSort
- UVa 10152 - ShellSort
- uva-10152 - ShellSort
- UVa 10152 - ShellSort
- uva 10152 - ShellSort
- 数据结构 uva-10152 - ShellSort
- UVa 10152 - ShellSort
- UVa 10152 - ShellSort
- uva 10152ShellSort
- uva 10152 ShellSort
- UVa 10152 - ShellSort
- uva 10152 ShellSort
- uva 10152 ShellSort
- 编写高性能 Web 应用程序的 10 个技巧
- “数据提供程序或其他服务返回 E_FAIL 状态” 的问题
- ASP.NET中进行消息处理(MSMQ) 一
- 【小事一箩筐】精彩推荐:11.17至11.21第1期
- Python 常用函数的 随时整理
- UVa Problem Solution: 10152 - ShellSort
- javascript的关键字
- Silverlight 2 学习笔记之事件的重复绑定问题
- 对象的序列化
- web2.0创业时代将终结
- PreparedStatement的setNull
- JavaScript execCommand用法举例
- 编写类String的构造函数、析构函数和赋值函数
- struts配置文件详细说明