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 giving the number of test cases. Each test case consist on an integer giving the number of turtles in the stack. The next 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 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

时间: 2024-10-23 10:39:10

ShellSort uva的相关文章

ShellSort UVA 10152

说说: 题意大概就是开始有一堆的字符串堆栈A,其中的任何一个字符串都可以从当前位置移动到栈顶,经过若干字符串的移动,最后形成了另一个字符串堆栈B,要求输出按顺序输出移动字符串最少的移动方案.其实解法挺简单的,既然要求的是最少的移动,那么就从B的底部开始逐个在A中从底部往上查找,并且要在A中标记查找位置,因为A中已经被查找过的字符串必然是要移动的.如此重复,直到B在原堆栈中找不到可以不移动的字符串.最后,B中剩下的都是移动过的字符串.因为字符串只能移动到顶部,因此从B中的当前位置逐个向顶部输出字符

UVa 10152 - ShellSort 题解

按他的方法排序,每次移动一个数到顶点,排成需要的序列. Problem D: 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! T

UVA ShellSort

题目如下: Problem D: 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

UVa 10152 - ShellSort

题目:给你一个现有字符串的序列,以及一个目标字符串的序列,每次操作可以把一个单词置顶, 问把当前串变成目标串,要操作几次. 分析:排序.因为每次可以使一个单词置顶,所以每个单词最多移动一次就可以变为目标串. 从后向前找到每个目标串元素对应元素的当前串位置(每个只能在"上一个"之前): 能找到的元素个数,就是不需要改动的个数,其他均要移动,对应目标串中剩余的元素: 剩余的目标串倒序输出,即为操作顺序. 定义两个指针分别从两串序列的尾部向头部移动,如果匹配成功同时前进: 不成功,当前传指针

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f

[UVa] Palindromes(401)

UVA - 401 Palindromes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDED

uva 401.Palindromes

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342 题目意思:给出一段字符串(大写字母+数字组成).判断是否为回文串 or 镜像串 or 回文镜像串 or 什么都不是.每个字母的镜像表格如下 Character Reverse Character Reverse Character Reverse A A M M Y Y B