UVa 11925:Generating Permutations(构造)
2015-08-23 16:25
363 查看
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=845&page=show_problem&problem=3076
题意:输入一个1 ~ n(1≤n≤300)(1 \le n \le 300)的排列,用不超过2n22n^2次操作把它变成升序。操作只有两种:交换前两个元素(操作1):把第一个元素移动到最后(操作2)。例如:输入排列为4,2,3,1,一个合法的操作序列为12122,具体操作是:4231->2431->4312->3412->4123->1234。(本段摘自《算法竞赛入门经典(第2版)》)。
关于题意以上表述有一些问题,题目要求的是从1,2,3,…,n的升序序列转化成题目所给的输入,其他的一样。
分析:
从升序转换成输入的序列不好做,因此倒过来处理,其中把第一个元素放到最后这种操作改成把最后一个元素放到第一个就好了。最后的只要把结果倒着输出就可以了。
代码:
题意:输入一个1 ~ n(1≤n≤300)(1 \le n \le 300)的排列,用不超过2n22n^2次操作把它变成升序。操作只有两种:交换前两个元素(操作1):把第一个元素移动到最后(操作2)。例如:输入排列为4,2,3,1,一个合法的操作序列为12122,具体操作是:4231->2431->4312->3412->4123->1234。(本段摘自《算法竞赛入门经典(第2版)》)。
关于题意以上表述有一些问题,题目要求的是从1,2,3,…,n的升序序列转化成题目所给的输入,其他的一样。
分析:
从升序转换成输入的序列不好做,因此倒过来处理,其中把第一个元素放到最后这种操作改成把最后一个元素放到第一个就好了。最后的只要把结果倒着输出就可以了。
代码:
#include <iostream> #include <fstream> #include <cstring> #include <vector> #include <queue> #include <cmath> #include <algorithm> #include <set> #include <string> using namespace std; const int maxn = 305; int n, tmp; int a[maxn]; string ans; bool judge() { for (int i = 0; i < n; ++i) if (a[i] != i + 1) return false; return true; } void change() { tmp = a[n - 1]; for (int i = n - 2; i >= 0; --i) a[i + 1] = a[i]; a[0] = tmp; } int main() { while (~scanf("%d", &n), n) { ans = ""; for (int i = 0; i < n; ++i) scanf("%d", &a[i]); if (n == 1) { printf("\n"); continue; } while (!judge()) { if (a[0] > a[1] && a[0] != n) { swap(a[0], a[1]); ans += "1"; } else { change(); ans += "2"; } } for (int i = ans.size() - 1; i >= 0; --i) printf("%c", ans[i]); printf("\n"); } return 0; }
相关文章推荐
- Android设计模式之工厂模式 Factory
- [LeetCode][JavaScript]Course Schedule
- 献给正在努力奋斗的年轻人
- Android高阶之事件传递机制的总结
- JS的一些笔记
- 纯css实现窗户玻璃雨滴逼真效果
- HDU 5418 状压DP + 最短路
- 150823_Xml_ImageView之scaleType
- An Intuitive Explanation of Fourier Theory
- 集合框架-TreeSet
- 排序算法之插入排序
- Lucene实践:Lucene总体设计架构
- 0823-阿里笔试
- Lucene实践:全文检索的基本原理
- 求连通分量
- gitosis clone项目时需要密码,全路径 解决办法
- Hibernate最小初始化 minimal constructor
- c++实现数据结构1.顺序表
- Qt android 调试与发布的一些东西
- Android横竖屏切换小结