ShellSort uva
ShellSort
He made each turtle stand on another one's back
And he piled them all up in a nine-turtle stack.
And then Yertle climbed up. He sat down on the pile.
What a wonderful view! He could see 'most a mile!
The Problem
King Yertle wishes to rearrange his turtle throne to place his highest-ranking nobles and closest advisors nearer to the top. A single operation is available to change the order of the turtles in the stack: a turtle
can crawl out of its position in the stack and climb up over the other turtles to sit on the top.
Given an original ordering of a turtle stack and a required ordering for the same turtle stack, your job is to determine a minimal sequence of operations that rearranges the original stack into the required stack.
The first line of the input consists of a single integer K giving the number of test cases. Each test case consist on an integer n giving the number of turtles in the stack. The next n lines
specify the original ordering of the turtle stack. Each of the lines contains the name of a turtle, starting with the turtle on the top of the stack and working down to the turtle at the bottom of the stack. Turtles have unique names, each of which is a string
of no more than eighty characters drawn from a character set consisting of the alphanumeric characters, the space character and the period (`.'). The next n lines in the input gives the desired ordering of the stack, once again by naming turtles from
top to bottom. Each test case consists of exactly 2n+1 lines in total. The number of turtles (n) will be less than or equal to two hundred.
For each test case, the output consists of a sequence of turtle names, one per line, indicating the order in which turtles are to leave their positions in the stack and crawl to the top. This sequence of operations
should transform the original stack into the required stack and should be as short as possible. If more than one solution of shortest length is possible, any of the solutions may be reported. Print a blank line after each test case.
Sample Input
2
3
Yertle
Duke of Earl
Sir Lancelot
Duke of Earl
Yertle
Sir Lancelot
9
Yertle
Duke of Earl
Sir Lancelot
Elizabeth Windsor
Michael Eisner
Richard M. Nixon
Mr. Rogers
Ford Perfect
Mack
Yertle
Richard M. Nixon
Sir Lancelot
Duke of Earl
Elizabeth Windsor
Michael Eisner
Mr. Rogers
Ford Perfect
Mack
Sample Output
Duke of Earl Sir Lancelot
Richard M. Nixon
Yertle
解决方式:通过观察,可把这些名字按目标位置标上序号。从上到下一次1......n,依照整理之前的顺序。把这些标号从后写入list容器。然后从头到尾遍历每一个位置,当这个元素比前面的最大值要小时,这个元素必须移动,但未必是操作最少。所以。当遍历完整个序列。选必须移动的元素之中的最大值移到开头。然后反复之前的动作,直到没有要移动的为止。用list模拟。用了2.9多秒。差点tle。不知还有没有更快的。
代码:
#include<iostream>
#include<list>
#include<map>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
list<int> L;//创建一个list容器
string name[220];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
map<string ,int >num;
map<int,string> str;
int n;
scanf("%d",&n);
getchar();
char Name[100];
for(int i=0;i<n;i++)
{
gets(Name);
name[i]=(string)Name;
}
for(int i=0;i<n;i++)
{
gets(Name);
num[(string)Name]=i+1;
str[i+1]=(string)Name;
}
L.clear();
for(int i=0;i<n;i++)
{
L.push_back(num[name[i]]);//从后把元素增加
}
list<int>::iterator j;
list<int>::iterator del;
list<int>::iterator te;//定义迭代器
int Max;
while(1){
bool flag=false;
Max=0;
for(j=L.begin();j!=L.end();j++)
{
te=max_element(L.begin(),j);//返回L.begin(),到j之间的最大值
if((*j)<(*te))
{
if(int(*j)>Max){
del=j;Max=int(*j);
}
flag=true;
}
}
if(!flag) break;
L.push_front(int(*del));//从前面增加
cout<<str[*del]<<endl;
L.erase(del);//删除del位置的元素
}
cout<<endl;
}
return 0;
}
ShellSort uva的更多相关文章
- uva 10152 ShellSort
//这个算法用到了"相对位置"的思想,并且就本题而言还有一个很重要的结论就是,假设 //移动了k个元素,那么这k个元素一定是最后结果的那个序列的前k个元素,而且易知, //越先移动 ...
- uva 10152 ShellSort 龟壳排序(希尔排序?)
今天状态总是很糟,这种题目卡了一天... 是不是休息时间太少了,头脑迟钝了... 名字叫希尔排序,我还以为跟它有关,还搜索了下资料. 只要找到trick就会发现是很水的题目.只要对比下就能找到哪些是移 ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
- UVA数学入门训练Round1[6]
UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...
- UVA - 1625 Color Length[序列DP 代价计算技巧]
UVA - 1625 Color Length 白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束 和模拟赛那道环形DP很想,计算这 ...
随机推荐
- JavaEE 数据库随机值插入测试
package com.jery.javaee.dbtest; import java.sql.Connection; import java.sql.DriverManager; import ja ...
- PMP 变更的流程
变更的流程: 内部变更: (团队成员提出)团队成员提出的变更,原则上拒绝,如果对客户和自己方都有好处不能拒绝1.内部变更 先分析影响 >> 2. 再提出变更请求 >> 3.变更 ...
- Android: 通过Runtime.getRuntime().exec调用底层Linux下的程序或脚本
Android Runtime使得直接调用底层Linux下的可执行程序或脚本成为可能 比如Linux下写个测试工具,直接编译后apk中通过Runtime来调用 或者写个脚本,apk中直接调用,省去中间 ...
- 使用SVN进行源码管理
阅读目录: 1.SVN服务端配置 1.1 创建版本库 1.2 创建用户 1.3 设置用户权限 2.SVN客户端使用 2.1 向SVN服务器中导入源码 2.1.1 直接通过TortoiseSVN向SVN ...
- nutz框架使用记录之Cnd.wrap
这是对Cnd.wrap 官方用法 , 直接硬编码 , [JAVA]List<Person> crowd = dao.query(Person.class, Cnd.wrap("n ...
- GridView, ListView 区别
ListView, GridView部分的类层次结构 AbsListView的xml属性 android:listSelector 当前item高亮时,显示的drawable android:draw ...
- File upload error - unable to create a temporary file
php上传图片的时候会报错: File upload error - unable to create a temporary file 文件上传错误 - 无法创建一个临时文件 你只需要打开你的php ...
- 【Linux相识相知】计算机的组成、linux发行版和哲学思想、基础命令和目录结构(FHS)
从今天开始,Frank将开始在博客上记录自己学习linux的点点滴滴,F初来乍到,还望各位大佬多多指教.本次博客的主要内容如下: 计算机基础:简要的描述了计算机的组成及其功能: linux初识:介绍了 ...
- ExcelHelper office 导出
要是服务器上没有装着excel 可以用c#导出excel表吗 2009-08-10 17:36 风之成 | 分类:办公软件 | 浏览2279次 租用的空间 服务器上没有装着office excel,可 ...
- python之高阶函数map/reduce
L = [] for n in [1, 2, 3, 4, 5, 6, 7, 8, 9]: L.append(f(n)) print(L) Python内建了map()和reduce()函数. 我们先看 ...