Problem UVA11925-Generating Permutations

Accept: 214  Submit: 1429
Time Limit: 1000 mSec

Problem Description

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 2n2 characters. If it is
 

 Sample Input

3 2 1 3
3 2 3 1
4 4 2 3 1
0
 

 Sample Output

1
2
12122

题解:这个题首先应该转换一下思维,考虑将给定串排成升序而不是将排好序的串变成给定串,这样会好想很多,注意如果这样思考的话,1操作就变成把最后一个数移到最前面,2操作不受影响。排序就是一个消除逆序对的过程,所以如果前面两个数是满足第一个数大于第二个数,那就要通过交换来消除这个逆序对(这样操作次数少),这里有个特殊情况就是第一个数是n并且第二个数是1,这时虽然构成逆序,但是是有可能通过把后面的数移到前面而使序列有序的,所以这时不要交换。

 #include <bits/stdc++.h>

 using namespace std;

 int n;
deque<int> seq;
string ans; bool check() {
for (int i = ; i < n; i++) {
if (seq[i] != i + ) return false;
}
return true;
} int main()
{
//freopen("input.txt", "r", stdin);
while (~scanf("%d", &n) && n) {
seq.clear();
ans = "";
int x;
for (int i = ; i < n; i++) {
scanf("%d", &x);
seq.push_back(x);
} while (true) {
if (seq[] == && check()) {
break;
}
if (seq[] < seq[] || seq[] == n && seq[] == ) {
seq.push_front(seq[n - ]);
seq.pop_back();
ans += '';
}
else {
swap(seq[], seq[]);
ans += '';
}
}
reverse(ans.begin(), ans.end());
cout << ans << endl;
}
return ;
}

UVA11925-Generating Permutations(贪心)的更多相关文章

  1. UVA-11925 Generating Permutations (逆向思维)

    题目大意:给出1~n的某个排列,问由升序变到这个排列最少需要几次操作.操作1:将头两个数交换:操作2:将头一个数移动最后一个位置. 题目分析:反过来考虑,将这个排列变为升序排列,那么这个变化过程实际上 ...

  2. uva11925 Generating Permutations

    逆序做,逆序输出 紫书上的描述有点问题 感觉很经典 ans.push_back(2); a.insert(a.begin(),a[n-1]); a.erase(a.end()-1); a.push_b ...

  3. CF722D. Generating Sets[贪心 STL]

    D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. hdu5338 ZZX and Permutations(贪心、线段树)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud ZZX and Permutations Time Limit: 6000/300 ...

  5. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心

    D. Generating Sets 题目连接: http://codeforces.com/contest/722/problem/D Description You are given a set ...

  6. hdu 5338 ZZX and Permutations (贪心+线段树+二分)

    ZZX and Permutations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/O ...

  7. Generating Sets 贪心

    H - Generating Sets Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  8. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D. Generating Sets 贪心+优先队列

    D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. UVa11925 Generating Premutations

    留坑(p.254) #include<cstdio> #include<cstring> #include<cstdlib> #include<algorit ...

  10. UVA 11925 - Generating Permutations

    题意: 给出一个1到n的排列,给出操作顺序,使升序排列能变为所给排列. 分析: 正常冒泡排序的想法.如果前两个数,前面的大于后面的,则换(特例是n,1不能换).否则,就用2的逆操作,把最后的数放前面. ...

随机推荐

  1. springMVC_06数据的处理

    一.提交数据的处理 *springmvc是单例的 1. 提交的域名称和处理方法的参数一致即可 提交的数据 处理方法 2.如果域名城和参数名不一致,在方法内加上域名称eg.(RequestParam(“ ...

  2. 3. mysql性能分析

    一.mysql query optimizer 1. mysql 中有专门负责优化 select 语句的优化器模块,主要功能:通过计算分析系统中收集的统计信息,为客户端的 Query 提供他认为最优的 ...

  3. 【LInux】查看Linux系统版本信息

    一.查看Linux内核版本命令(两种方法): 1.cat /proc/version [root@S-CentOS home]# cat /proc/versionLinux version 2.6. ...

  4. 关于java中反射的小结

    一.Class 1. Class是一个类,封装了当前对象所对应的类的信息 2.小写class表示是一个类类型,大写Class表示这个类的名称 3.对于每个类而言,JRE 都为其保留一个不变的 Clas ...

  5. 汇编语言--微机CPU的指令系统(五)(比较运算指令)

    (7)比较运算指令 在程序中,我们要时常根据某个变量或表达式的取值去执行不同指令,从而使程序表现出有不同的功能.为了配合这样的操作,在CPU的指令系统中提供了各种不同的比较指令.通过这些比较指令的执行 ...

  6. ajaxJson(常用)

    function ajaxJson(method, url, data, callback) { var options = { type: method, url: url, dataType: ' ...

  7. POJ 2311 Cutting Game(二维SG+Multi-Nim)

    Cutting Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4798   Accepted: 1756 Desc ...

  8. 洛谷P4592 [TJOI2018]异或(可持久化01Trie)

    题意 题目链接 可持久化01Trie板子题 对于两个操作分别开就行了 #include<bits/stdc++.h> using namespace std; const int MAXN ...

  9. spring boot 基础 2018年5月3日

    主包下运行类@SpringBootApplication  此注解是核心注解,源码如下 @Target({ElementType.TYPE}) @Retention(RetentionPolicy.R ...

  10. git 入门教程之撤销更改

    撤销更改 相信你已经了解了 git 的基本概念,也清楚了工作区,暂存区和版本库的关系,现在让我们用所学的知识继解决实际问题吧! 背景 正常看得见的目录是我们最为熟悉的工作区,在工作中不可能总是100% ...