A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these integers. Your task is to generate a given permutation from the initial arrangement 1, 2, 3, . . . , n using only two simple operations.

•  Operation 1: You may swap the first two numbers. For example, this would change the arrangement 3,2,4,5,1 to 2,3,4,5,1.

• Operation 2: You may move the first number to the end of the arrangement. For example, this would change the arrangement 3,2,4,5,1 to 2,4,5,1,3.

Input

The input consists of a number of test cases. Each test case begins with a single integer n between 1 and 300. On the same line, a permutation of integers 1 through n is given where consecutive integers are separated by a single space. Input is terminated by a line containing ‘0’ which should not be processed.

Output

For each test case you are to output a string on a single line that describes a sequence of operations. The string itself should consist only of the characters ‘1’ and ‘2’. This string should be such that if we start with the initial arrangement 1, 2, 3, . . . , n − 1, n and successively apply rules 1 and 2 according to the order they appear in the output, then the resulting permutation is identical to the input permutation. The output string does not necessarily need to be the shortest such string, but it must be no longer than 2n 2 characters. If it is possible to generate the permutation using 0 operations, then you may simply output a blank line.

Sample Input

3 2 1 3

3 2 3 1

4 4 2 3 1

0

Sample Output

1

2

12122

题意:有一组序列,它原来初始排列为1 2 3 4 5。。。。n,经过操作1和操作2的可以变成输入中所给出的序列,要求输出经过了哪一些操作

如 123经过操作1可得213

思路:构造法。。。哪有算法可言,幸好题目说道不一定要求输入最少的那一系列操作,那么就为程序寻找一个进行操作1还是操作2的判定条件,条件是不能执行操作1就执行操作2,那么什么时候不能执行操作1呢,答案是前面两个数字有序时,无序时就交换呗,有序时你想换也换不了。。。重点在于这样的判断中有一个特殊情况,那就是如果前面两个数中有1时,那么无论他们有没有序,都不能执行操作1,否则会陷入循环,怎么也得不出结果了

其余的,模拟吧。。。

#include"iostream"
#include"cstdio"
#include"cstring"
using namespace std; const int maxn=300+10; int a[maxn];
int n; string ans; void swap1()
{
int t;
t=a[0];
a[0]=a[1];
a[1]=t;
} void swap2()
{
int t;
t=a[n-1];
for(int i=n-1;i>0;i--)
{a[i]=a[i-1];}
a[0]=t;
} bool check()
{
for(int i=0;i<n;i++)
{
if(a[i]!=i+1) return false;
}
return true;
} void print()
{
for(int i=ans.length()-1;i>=0;i--)
cout<<ans[i];
} int main()
{
while(cin>>n&&n)
{
memset(a,0,sizeof(a));
ans.clear();
for(int i=0;i<n;i++)
cin>>a[i];
int T=2*n*n;
while(T--)
{
if(check()) {print();break;}
if(a[0]>a[1]&&a[0]!=1&&a[1]!=1) {swap1();ans+='1';}
else {swap2();ans+='2';}
}
cout<<endl;
}
return 0;
}

集训第四周(高效算法设计)O题 (构造题)的更多相关文章

  1. 集训第四周(高效算法设计)A题 Ultra-QuickSort

    原题poj 2299:http://poj.org/problem?id=2299 题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,归 ...

  2. 集训第四周(高效算法设计)P题 (构造题)

    Description   There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<te ...

  3. 集训第四周(高效算法设计)N题 (二分查找优化题)

    原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...

  4. 集训第四周(高效算法设计)M题 (扫描法)

    原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...

  5. 集训第四周(高效算法设计)I题 (贪心)

    Description Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshe ...

  6. 集训第四周(高效算法设计)E题 (区间覆盖问题)

    UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419 只能说这道题和D题是一模一样的,不过要进行转化, ...

  7. 集训第四周(高效算法设计)D题 (区间覆盖问题)

    原题 UVA10020  :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19688 经典的贪心问题,区间上贪心当然是右区间越 ...

  8. 集训第四周(高效算法设计)L题 (背包贪心)

    Description   John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...

  9. 集训第四周(高效算法设计)K题 (滑窗问题)

    UVA 11572 唯一的雪花 题意:给你从1到n的数组,要求求得其中的最长连续不重复子序列,经典的滑窗问题,方法是维护一个窗口,设置左框和右框,然后不断的进行维护和更新 方法一: #include& ...

随机推荐

  1. Luogu P1160队列安排【链表/老文搬家】By cellur925

    原文发表于2018-04-15 08:15:09,我的luogu博客qwq. 看到题以后,要求维护一个可在任意位置修改添加删除元素的序列,那么显然我们可以用到链表. 然而本蒟蒻不久前刚刚学会链表.链表 ...

  2. jQuery笔记之animate中的queue

    队列 队列的执行顺序 queue() dequeue() 输出对象里面的内容 依次出队 不过这样写太麻烦了,因为每次都要输出,所以我们看下面的方法 运用到队列输出的 <!DOCTYPE html ...

  3. spring-retry简单demo(附完整代码)

    重试 最近项目要用到重试.开始想自己写,后来想用RetryTemplate,最后想到既然项目用了springboot,还是直接集成spring-retry把. 代码 Application packa ...

  4. iOS NSDictionary <--> NSString(JSON) in Objc

    NSDictionary --> NSString + (NSString*)stringINJSONFormatForObject:(id)obj { NSData *jsonData = [ ...

  5. Python上下文管理器(Context managers)

    上下文管理器(Context managers) 上下文管理器允许你在有需要的时候,精确地分配和释放资源. 使用上下文管理器最广泛的案例就是with语句了.想象下你有两个需要结对执行的相关操作,然后还 ...

  6. [CTSC2000]丘比特的烦恼

    Description 随着社会的不断发展,人与人之间的感情越来越功利化.最近,爱神丘比特发现,爱情也已不再是完全纯洁的了.这使得丘比特很是苦恼,他越来越难找到合适的男女,并向他们射去丘比特之箭.于是 ...

  7. HDU 6096 树套树

    思路: 网上的题解有AC自动机的,有trie树的,还有(乱搞?)的 首先把输入的那n个串按照字典序排序, 把n个串翻转以后再按照字典序排序 这样我们发现, 查的前缀在字典序排序后是一段区间, 查的后缀 ...

  8. 人工智能(七)逻辑Agent

    一.逻辑 逻辑是一种可以从中找出结论的形式化语言. 句法(规则)用语言定义句子. 语义定义句子的含义.定义一个句子的真假性. 二.蕴含 即一个事情逻辑上是另一个事情的必然结果:KB ╞ α 知识库KB ...

  9. 题解报告:hdu 1098 Ignatius's puzzle

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1098 题目中文是这样的: 伊格内修斯在数学上很差,他遇到了一个难题,所以他别无选择,只能上诉埃迪. 这 ...

  10. Eclipse快捷键集合

    ***eclipse查看哪个方法被调用:选中,右键选择Open call Hierarchy     选择要查看的方法: ctrl + alt + h       查看一个类被那些类继承或者实现: F ...